colors helping a user manually enter project paths

This commit is contained in:
fros4943 2007-04-11 14:00:27 +00:00
parent f2f5ea682b
commit eeed4115c4
2 changed files with 149 additions and 6 deletions

View file

@ -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);
}
}
});

View file

@ -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);
}
}