From eeed4115c48fcf59c829e1b44606f7f652b5b546 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 11 Apr 2007 14:00:27 +0000 Subject: [PATCH] colors helping a user manually enter project paths --- .../dialogs/ProjectDirectoriesDialog.java | 16 +- .../dialogs/ProjectDirectoryInputDialog.java | 139 ++++++++++++++++++ 2 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoryInputDialog.java diff --git a/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoriesDialog.java b/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoriesDialog.java index d34fe5a16..176a5768d 100644 --- a/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoriesDialog.java +++ b/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoriesDialog.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ProjectDirectoriesDialog.java,v 1.2 2007/04/02 10:32:37 fros4943 Exp $ + * $Id: ProjectDirectoriesDialog.java,v 1.3 2007/04/11 14:00:27 fros4943 Exp $ */ package se.sics.cooja.dialogs; @@ -306,11 +306,15 @@ public class ProjectDirectoriesDialog extends JDialog { button = new JButton("Add manually"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - String newProjectPath = JOptionPane.showInputDialog(myDialog, - "Enter path to project directory", "Enter path", - JOptionPane.QUESTION_MESSAGE); - if (newProjectPath != null) { - addProjectDir(new File(newProjectPath)); + ProjectDirectoryInputDialog pathDialog = new ProjectDirectoryInputDialog(myParentFrame); + pathDialog.pack(); + + pathDialog.setLocationRelativeTo(myParentFrame); + pathDialog.setVisible(true); + + File projectPath = pathDialog.getProjectDirectory(); + if (projectPath != null) { + addProjectDir(projectPath); } } }); diff --git a/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoryInputDialog.java b/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoryInputDialog.java new file mode 100644 index 000000000..dfab5a946 --- /dev/null +++ b/tools/cooja/java/se/sics/cooja/dialogs/ProjectDirectoryInputDialog.java @@ -0,0 +1,139 @@ +package se.sics.cooja.dialogs; + +import java.beans.*; +import java.io.File; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.*; +import org.apache.log4j.Logger; + +import se.sics.cooja.GUI; + +class ProjectDirectoryInputDialog extends JDialog implements ActionListener, PropertyChangeListener { + private static Logger logger = Logger.getLogger(ProjectDirectoryInputDialog.class); + + private File projectDirectoryPath = null; + private JTextField textField; + + private JOptionPane optionPane; + + private String buttonAdd = "Add"; + private String buttonCancel = "Cancel"; + + public ProjectDirectoryInputDialog(Frame parent) { + super(parent, true); + + setTitle("Enter path"); + + textField = new JTextField(10); + textField.getDocument().addDocumentListener(new DocumentListener() { + public void insertUpdate(DocumentEvent e) { + pathChanged(); + } + public void removeUpdate(DocumentEvent e) { + pathChanged(); + } + public void changedUpdate(DocumentEvent e) { + pathChanged(); + } + }); + + Object[] objects = {"Enter path to project directory", textField}; + Object[] options = {buttonAdd, buttonCancel}; + + optionPane = new JOptionPane(objects, + JOptionPane.QUESTION_MESSAGE, + JOptionPane.YES_NO_OPTION, + null, + options, + options[0]); + + setContentPane(optionPane); + + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent we) { + optionPane.setValue(new Integer( + JOptionPane.CLOSED_OPTION)); + } + }); + + addComponentListener(new ComponentAdapter() { + public void componentShown(ComponentEvent ce) { + textField.requestFocusInWindow(); + } + }); + + textField.addActionListener(this); + optionPane.addPropertyChangeListener(this); + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + pathChanged(); + } + }); + } + + public File getProjectDirectory() { + return projectDirectoryPath; + } + + private void pathChanged() { + String newPath = textField.getText(); + File projectDir = new File(newPath); + if (!projectDir.exists()) { + textField.setBackground(Color.RED); + textField.setToolTipText("Directory does not exist"); + return; + } + + if (!projectDir.isDirectory()) { + textField.setBackground(Color.RED); + textField.setToolTipText("Not a directory"); + return; + } + + if (!new File(projectDir, GUI.PROJECT_CONFIG_FILENAME).exists()) { + textField.setBackground(Color.LIGHT_GRAY); + textField.setToolTipText("No configuration file found"); + return; + } + + textField.setBackground(Color.WHITE); + textField.setToolTipText(""); + } + + public void actionPerformed(ActionEvent e) { + optionPane.setValue(buttonAdd); + } + + public void propertyChange(PropertyChangeEvent e) { + String prop = e.getPropertyName(); + if (isVisible() && (e.getSource() == optionPane) + && (JOptionPane.VALUE_PROPERTY.equals(prop) || + JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) { + Object value = optionPane.getValue(); + + if (value == JOptionPane.UNINITIALIZED_VALUE) { + return; + } + + optionPane.setValue( + JOptionPane.UNINITIALIZED_VALUE); + + if (buttonAdd.equals(value)) { + projectDirectoryPath = new File(textField.getText()); + close(); + } else { + projectDirectoryPath = null; + close(); + } + } + } + + public void close() { + textField.setText(null); + setVisible(false); + } +}