1 package com.opensymphony.workflow.designer.swing; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.ActionListener ; 6 import java.io.File ; 7 import javax.swing.*; 8 import javax.swing.event.EventListenerList ; 9 10 import com.opensymphony.workflow.designer.Utils; 11 12 public class FileField extends JPanel 13 { 14 private final JTextField field = new JTextField(); 15 private final FixedButton button = new FixedButton(field); 16 private String suffix; 17 private String description; 18 private final int type; 19 private boolean isSave; 20 private boolean buttonEnabled; 21 private File file; 22 protected EventListenerList listenerList = new EventListenerList (); 23 24 28 protected transient ActionEvent actionEvent; 29 30 public void actionPerformed(ActionEvent e) 31 { 32 } 33 34 public FileField(int type, boolean save, String suffix, String description) 35 { 36 super(new BorderLayout()); 37 this.type = type; 38 this.isSave = save; 39 this.suffix = suffix; 40 this.description = description; 41 buttonEnabled = true; 42 add(field, BorderLayout.CENTER); 43 add(button, BorderLayout.EAST); 44 button.addActionListener(new ActionListener () 45 { 46 public void actionPerformed(ActionEvent e) 47 { 48 File file = Utils.promptUserForFile(FileField.this, FileField.this.type, FileField.this.isSave, 49 FileField.this.suffix, FileField.this.description); 50 if(file != null) 51 { 52 field.setText(file.getAbsolutePath()); 53 FileField.this.file = file; 54 fireStateChanged(); 55 } 56 } 57 }); 58 } 59 60 65 public void addActionListener(ActionListener l) 66 { 67 listenerList.add(ActionListener .class, l); 68 } 69 70 77 protected void fireStateChanged() 78 { 79 Object [] listeners = listenerList.getListenerList(); 81 for(int i = listeners.length - 2; i >= 0; i -= 2) 84 { 85 if(listeners[i] == ActionListener .class) 86 { 87 if(actionEvent == null) 89 actionEvent = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, "fileselected"); 90 ((ActionListener )listeners[i + 1]).actionPerformed(actionEvent); 91 } 92 } 93 } 94 95 100 public void removeActionListener(ActionListener l) 101 { 102 listenerList.remove(ActionListener .class, l); 103 } 104 105 112 public ActionListener [] getActionListeners() 113 { 114 return (ActionListener [])(listenerList.getListeners(ActionListener .class)); 115 } 116 117 public void setButtonIcon(Icon icon) 118 { 119 button.setIcon(icon); 120 } 121 122 public void setButtonEnabled(boolean flag) 123 { 124 buttonEnabled = flag; 125 setEnabled(isEnabled()); 126 } 127 128 public void setEnabled(boolean enabled) 129 { 130 super.setEnabled(enabled); 131 button.setEnabled(enabled && buttonEnabled); 132 field.setEnabled(enabled); 133 } 134 135 public FixedButton getButton() 136 { 137 return button; 138 } 139 140 public JTextField getTextField() 141 { 142 return field; 143 } 144 145 public String getText() 146 { 147 return field.getText().trim(); 148 } 149 150 public void setText(String s) 151 { 152 field.setText(s); 153 } 154 155 public File getFile() 156 { 157 return file; 158 } 159 160 public void setTextFieldPreferredWidth(int i) 161 { 162 Dimension dimension = field.getPreferredSize(); 163 FontMetrics fontmetrics = field.getFontMetrics(field.getFont()); 164 dimension.width = fontmetrics.charWidth('a') * i; 165 field.setPreferredSize(dimension); 166 } 167 168 public boolean isEditable() 169 { 170 return field.isEditable(); 171 } 172 173 public void setEditable(boolean flag) 174 { 175 field.setEditable(flag); 176 } 177 } 178 | Popular Tags |