Compare commits
1 Commits
zhangsanbr
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
a9c0987753 | 3 years ago |
@ -1,236 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* @version 1.33 2007-06-12
|
||||
* @author Cay Horstmann
|
||||
*/
|
||||
public class DataExchangeTest
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
EventQueue.invokeLater(new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
DataExchangeFrame frame = new DataExchangeFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A frame with a menu whose File->Connect action shows a password dialog.
|
||||
*/
|
||||
class DataExchangeFrame extends JFrame
|
||||
{
|
||||
public DataExchangeFrame()
|
||||
{
|
||||
setTitle("DataExchangeTest");
|
||||
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
||||
|
||||
// construct a File menu
|
||||
|
||||
JMenuBar mbar = new JMenuBar();
|
||||
setJMenuBar(mbar);
|
||||
JMenu fileMenu = new JMenu("File");
|
||||
mbar.add(fileMenu);
|
||||
|
||||
// add Connect and Exit menu items
|
||||
|
||||
JMenuItem connectItem = new JMenuItem("Connect");
|
||||
connectItem.addActionListener(new ConnectAction());
|
||||
fileMenu.add(connectItem);
|
||||
|
||||
// The Exit item exits the program
|
||||
|
||||
JMenuItem exitItem = new JMenuItem("Exit");
|
||||
exitItem.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
fileMenu.add(exitItem);
|
||||
|
||||
textArea = new JTextArea();
|
||||
add(new JScrollPane(textArea), BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
public static final int DEFAULT_WIDTH = 300;
|
||||
public static final int DEFAULT_HEIGHT = 200;
|
||||
|
||||
private PasswordChooser dialog = null;
|
||||
private JTextArea textArea;
|
||||
|
||||
/**
|
||||
* The Connect action pops up the password dialog.
|
||||
*/
|
||||
|
||||
private class ConnectAction implements ActionListener
|
||||
{
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
// if first time, construct dialog
|
||||
|
||||
if (dialog == null) dialog = new PasswordChooser();
|
||||
|
||||
// set default values
|
||||
dialog.setUser(new User("yourname", null));
|
||||
|
||||
// pop up dialog
|
||||
if (dialog.showDialog(DataExchangeFrame.this, "Connect"))
|
||||
{
|
||||
// if accepted, retrieve user input
|
||||
User u = dialog.getUser();
|
||||
textArea.append("user name = " + u.getName() + ", password = "
|
||||
+ (new String(u.getPassword())) + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A password chooser that is shown inside a dialog
|
||||
*/
|
||||
class PasswordChooser extends JPanel
|
||||
{
|
||||
public PasswordChooser()
|
||||
{
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// construct a panel with user name and password fields
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridLayout(2, 2));
|
||||
panel.add(new JLabel("User name:"));
|
||||
panel.add(username = new JTextField(""));
|
||||
panel.add(new JLabel("Password:"));
|
||||
panel.add(password = new JPasswordField(""));
|
||||
add(panel, BorderLayout.CENTER);
|
||||
|
||||
// create Ok and Cancel buttons that terminate the dialog
|
||||
|
||||
okButton = new JButton("Ok");
|
||||
okButton.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
ok = true;
|
||||
dialog.setVisible(false);
|
||||
}
|
||||
});
|
||||
|
||||
JButton cancelButton = new JButton("Cancel");
|
||||
cancelButton.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
dialog.setVisible(false);
|
||||
}
|
||||
});
|
||||
|
||||
// add buttons to southern border
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.add(okButton);
|
||||
buttonPanel.add(cancelButton);
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the dialog defaults.
|
||||
* @param u the default user information
|
||||
*/
|
||||
public void setUser(User u)
|
||||
{
|
||||
username.setText(u.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dialog entries.
|
||||
* @return a User object whose state represents the dialog entries
|
||||
*/
|
||||
public User getUser()
|
||||
{
|
||||
return new User(username.getText(), password.getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the chooser panel in a dialog
|
||||
* @param parent a component in the owner frame or null
|
||||
* @param title the dialog window title
|
||||
*/
|
||||
public boolean showDialog(Component parent, String title)
|
||||
{
|
||||
ok = false;
|
||||
|
||||
// locate the owner frame
|
||||
|
||||
Frame owner = null;
|
||||
if (parent instanceof Frame) owner = (Frame) parent;
|
||||
else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
|
||||
|
||||
// if first time, or if owner has changed, make new dialog
|
||||
|
||||
if (dialog == null || dialog.getOwner() != owner)
|
||||
{
|
||||
dialog = new JDialog(owner, true);
|
||||
dialog.add(this);
|
||||
dialog.getRootPane().setDefaultButton(okButton);
|
||||
dialog.pack();
|
||||
}
|
||||
|
||||
// set title and show dialog
|
||||
|
||||
dialog.setTitle(title);
|
||||
dialog.setVisible(true);
|
||||
return ok;
|
||||
}
|
||||
|
||||
private JTextField username;
|
||||
private JPasswordField password;
|
||||
private JButton okButton;
|
||||
private boolean ok;
|
||||
private JDialog dialog;
|
||||
}
|
||||
|
||||
/**
|
||||
* A user has a name and password. For security reasons, the password is stored as a char[], not a
|
||||
* String.
|
||||
*/
|
||||
class User
|
||||
{
|
||||
public User(String aName, char[] aPassword)
|
||||
{
|
||||
name = aName;
|
||||
password = aPassword;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public char[] getPassword()
|
||||
{
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setName(String aName)
|
||||
{
|
||||
name = aName;
|
||||
}
|
||||
|
||||
public void setPassword(char[] aPassword)
|
||||
{
|
||||
password = aPassword;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private char[] password;
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.micode.notes.ui;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import net.micode.notes.R;
|
||||
import net.micode.notes.ui.DateTimePicker;
|
||||
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
||||
|
||||
private Calendar mDate = Calendar.getInstance();
|
||||
private boolean mIs24HourView;
|
||||
private OnDateTimeSetListener mOnDateTimeSetListener;
|
||||
private DateTimePicker mDateTimePicker;
|
||||
|
||||
public interface OnDateTimeSetListener {
|
||||
void OnDateTimeSet(AlertDialog dialog, long date);
|
||||
}
|
||||
|
||||
public DateTimePickerDialog(Context context, long date) {
|
||||
super(context);
|
||||
mDateTimePicker = new DateTimePicker(context);
|
||||
setView(mDateTimePicker);
|
||||
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
|
||||
public void onDateTimeChanged(DateTimePicker view, int year, int month,
|
||||
int dayOfMonth, int hourOfDay, int minute) {
|
||||
mDate.set(Calendar.YEAR, year);
|
||||
mDate.set(Calendar.MONTH, month);
|
||||
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
||||
mDate.set(Calendar.MINUTE, minute);
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
}
|
||||
});
|
||||
mDate.setTimeInMillis(date);
|
||||
mDate.set(Calendar.SECOND, 0);
|
||||
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
|
||||
setButton(context.getString(R.string.datetime_dialog_ok), this);
|
||||
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
|
||||
set24HourView(DateFormat.is24HourFormat(this.getContext()));
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
}
|
||||
|
||||
public void set24HourView(boolean is24HourView) {
|
||||
mIs24HourView = is24HourView;
|
||||
}
|
||||
|
||||
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
||||
mOnDateTimeSetListener = callBack;
|
||||
}
|
||||
|
||||
private void updateTitle(long date) {
|
||||
int flag =
|
||||
DateUtils.FORMAT_SHOW_YEAR |
|
||||
DateUtils.FORMAT_SHOW_DATE |
|
||||
DateUtils.FORMAT_SHOW_TIME;
|
||||
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
|
||||
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
if (mOnDateTimeSetListener != null) {
|
||||
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue