1 23 package org.enhydra.kelp.common.xmlc; 24 25 import org.enhydra.tool.common.DialogPanel; 27 import org.enhydra.tool.common.ExtensionFilter; 28 import org.enhydra.tool.common.PathHandle; 29 30 import org.enhydra.kelp.common.Constants; 32 import org.enhydra.kelp.common.node.OtterNode; 33 import org.enhydra.kelp.common.node.OtterFileNode; 34 import org.enhydra.kelp.common.properties.XMLCParameterViewer; 35 36 import javax.swing.*; 38 import javax.swing.border.TitledBorder ; 39 import java.awt.event.*; 40 import java.io.File ; 41 import java.awt.*; 42 import java.beans.*; 43 import java.util.ResourceBundle ; 44 45 51 public class XMLCOptionPanel extends JPanel { 52 static ResourceBundle res = 53 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private GridBagLayout layoutMain = new GridBagLayout(); 55 private OtterNode node = null; 56 private JTextField textFile = null; 57 private JButton buttonBrowse = null; 58 private LocalButtonListener buttonListener = null; 59 private TitledBorder border = null; 60 private JTextField textCommandLine = null; 61 private JLabel labelCommandLine = null; 62 private JLabel labelFile = null; 63 private JButton buttonClear = null; 64 private JButton buttonTool = null; 65 66 70 public XMLCOptionPanel() { 71 try { 72 jbInit(); 73 pmInit(); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 } 78 79 80 86 public void setEnabled(boolean b) { 87 super.setEnabled(b); 88 textCommandLine.setEnabled(b); 89 textFile.setEnabled(b); 90 buttonTool.setEnabled(b); 91 buttonBrowse.setEnabled(b); 92 buttonClear.setEnabled(b); 93 if (b) { 94 textCommandLine.setBackground(SystemColor.info); 95 textFile.setBackground(SystemColor.info); 96 } else { 97 textCommandLine.setBackground(SystemColor.text); 98 textFile.setBackground(SystemColor.text); 99 } 100 } 101 102 108 public OtterNode getNode() { 109 return node; 110 } 111 112 118 public void setNode(OtterNode n) { 119 node = n; 120 } 121 122 126 public void readProperties() { 127 String filename = null; 128 String parameters = null; 129 130 if (node == null) { 131 buttonBrowse.setEnabled(false); 132 } else { 133 buttonBrowse.setEnabled(true); 134 filename = node.getXMLCOptionFilePath(); 135 parameters = node.getXMLCParameters(); 136 } 137 if (filename != null) { 138 File file = new File (filename); 139 140 if (file.isFile()) { 141 textFile.setText(filename); 142 } 143 } 144 if (parameters != null) { 145 textCommandLine.setText(parameters); 146 } 147 } 148 149 153 public void writeProperties() { 154 String parameters = textCommandLine.getText().trim(); 155 156 if (node != null) { 157 node.setXMLCParameters(parameters); 158 node.setXMLCOptionFilePath(getValidFilename()); 159 } 160 } 161 162 168 private String getValidFilename() { 169 PathHandle path = null; 170 String returnPath = new String (); 171 172 path = PathHandle.createPathHandle(textFile.getText()); 173 if (path.isFile() && path.hasExtension(Constants.TYPE_XMLC)) { 174 returnPath = path.getPath(); 175 textFile.setText(path.getPath()); 176 } else { 177 textFile.setText(new String ()); 178 } 179 return returnPath; 180 } 181 182 188 private boolean isValidFilename() { 189 return (getValidFilename().length() > 0); 190 } 191 192 196 private void browseForFile() { 197 JFileChooser chooser; 198 ExtensionFilter filter; 199 File file; 200 String fileDir = new String (); 201 String fileName = new String (); 202 203 filter = new ExtensionFilter(); 204 filter.addExtension(Constants.TYPE_XMLC); 205 filter.setDescriptionTitle(res.getString("filter_DescriptionTitle")); 206 chooser = new JFileChooser(); 207 if (textFile.getText().trim().length() > 0) { 208 chooser.setCurrentDirectory(new File (textFile.getText())); 209 } else { 210 PathHandle ph = null; 211 212 if (node instanceof OtterFileNode) { 213 OtterFileNode fileNode = (OtterFileNode) node; 214 ph = PathHandle.createPathHandle(fileNode.getFilePath()); 215 if (ph.isEmpty()) { 216 ph = null; 217 } else { 218 ph = ph.getParent(); 219 } 220 } 221 if (ph == null) { 222 ph = PathHandle.createPathHandle(node.getProject().getRootPath()); 223 } 224 chooser.setCurrentDirectory(ph.getFile()); 225 } 226 chooser.setFileFilter(filter); 227 chooser.setDialogTitle(res.getString("chooser_DialogTitle")); 228 chooser.setApproveButtonText(res.getString("OK")); 229 int v = chooser.showOpenDialog(this); 230 231 this.requestFocus(); 232 buttonBrowse.requestFocus(); 233 if (v == JFileChooser.APPROVE_OPTION) { 234 if (chooser.getSelectedFile() == null 235 || (!chooser.getSelectedFile().isFile())) { 236 textFile.setText(new String ()); 237 } else { 238 fileDir = chooser.getCurrentDirectory().toString(); 239 fileDir = stripSlash(fileDir); 240 fileName = chooser.getSelectedFile().getName(); 241 textFile.setText(fileDir + File.separator + fileName); 242 } 243 } 244 chooser.removeAll(); 245 chooser = null; 246 } 247 248 256 private String stripSlash(String dir) { 257 String stripped = new String (dir); 258 259 if (dir != null) { 260 if (dir.length() > 0) { 261 if (dir.endsWith(File.separator)) { 262 stripped = dir.substring(0, dir.length() - 1); 263 } 264 } 265 } 266 return stripped; 267 } 268 269 private void editParameters() { 270 String params = new String (); 271 XMLCParameterViewer viewer = null; 272 273 params = textCommandLine.getText(); 274 viewer = new XMLCParameterViewer(); 275 viewer.setParameters(params); 276 viewer.setOwner((Dialog) getTopLevelAncestor()); 277 if (viewer.showDialog() == DialogPanel.OK_OPTION) { 278 params = viewer.getParameters(); 279 textCommandLine.setText(params); 280 } 281 } 282 283 287 private void pmInit() { 288 textCommandLine.setText(new String ()); 289 buttonListener = new LocalButtonListener(); 290 buttonBrowse.addActionListener(buttonListener); 291 buttonClear.addActionListener(buttonListener); 292 buttonTool.addActionListener(buttonListener); 293 setEnabled(true); 294 } 295 296 302 private void jbInit() throws Exception { 303 buttonBrowse = 304 (JButton) Beans.instantiate(getClass().getClassLoader(), 305 JButton.class.getName()); 306 textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(), 307 JTextField.class.getName()); 308 border = 309 BorderFactory.createTitledBorder(res.getString("XMLC_Options")); 310 textCommandLine = 311 (JTextField) Beans.instantiate(getClass().getClassLoader(), 312 JTextField.class.getName()); 313 labelCommandLine = 314 (JLabel) Beans.instantiate(getClass().getClassLoader(), 315 JLabel.class.getName()); 316 labelFile = (JLabel) Beans.instantiate(getClass().getClassLoader(), 317 JLabel.class.getName()); 318 buttonClear = (JButton) Beans.instantiate(getClass().getClassLoader(), 319 JButton.class.getName()); 320 buttonTool = (JButton) Beans.instantiate(getClass().getClassLoader(), 321 JButton.class.getName()); 322 textFile.setMinimumSize(new Dimension(100, 21)); 323 textFile.setPreferredSize(new Dimension(100, 21)); 324 textFile.setEditable(false); 325 buttonBrowse.setText(res.getString("Browse")); 326 labelCommandLine.setText(res.getString("labelCommandLine_Text")); 327 labelFile.setText(res.getString("labelFile_Text")); 328 buttonClear.setText(res.getString("Clear")); 329 buttonTool.setText("..."); this.setLayout(layoutMain); 331 this.setBorder(border); 332 this.add(textFile, 333 new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0, 334 GridBagConstraints.WEST, 335 GridBagConstraints.HORIZONTAL, 336 new Insets(1, 2, 2, 2), 100, 0)); 337 this.add(buttonBrowse, 338 new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, 339 GridBagConstraints.CENTER, 340 GridBagConstraints.NONE, 341 new Insets(1, 2, 2, 2), 0, 0)); 342 this.add(labelCommandLine, 343 new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, 344 GridBagConstraints.WEST, 345 GridBagConstraints.HORIZONTAL, 346 new Insets(2, 2, 1, 2), 0, 0)); 347 this.add(textCommandLine, 348 new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0, 349 GridBagConstraints.WEST, 350 GridBagConstraints.HORIZONTAL, 351 new Insets(1, 2, 2, 2), 0, 0)); 352 this.add(labelFile, 353 new GridBagConstraints(0, 3, 4, 1, 0.0, 0.0, 354 GridBagConstraints.WEST, 355 GridBagConstraints.HORIZONTAL, 356 new Insets(2, 2, 1, 2), 0, 0)); 357 this.add(buttonClear, 358 new GridBagConstraints(2, 4, 2, 1, 0.0, 0.0, 359 GridBagConstraints.CENTER, 360 GridBagConstraints.NONE, 361 new Insets(1, 2, 2, 2), 0, 0)); 362 this.add(buttonTool, 363 new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, 364 GridBagConstraints.CENTER, 365 GridBagConstraints.NONE, 366 new Insets(1, 2, 2, 2), 0, 0)); 367 } 368 369 private class LocalButtonListener implements ActionListener { 371 public void actionPerformed(ActionEvent e) { 372 Object source = e.getSource(); 373 374 if (source == buttonBrowse) { 375 browseForFile(); 376 } else if (source == buttonClear) { 377 textFile.setText(new String ()); 378 } else if (source == buttonTool) { 379 editParameters(); 380 } 381 } 382 383 } 384 } 385 | Popular Tags |