View Javadoc
1 /*** 2 * ArgvDialog.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 04.10.2002 pko creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency; 31 32 import ch.elca.dependency.gui.MyFileFilter; 33 import ch.elca.dependency.util.IconGrabber; 34 35 import java.awt.*; 36 import java.awt.event.*; 37 import java.io.File; 38 import java.util.ArrayList; 39 import javax.swing.*; 40 import javax.swing.border.*; 41 42 import bry.techbase.configuration.CommandLineConfiguration; 43 import bry.techbase.configuration.Configuration; 44 import info.clearthought.layout.TableLayout; 45 46 /*** 47 * Class <code>ArgvDialog</code> used by the DPTool class to give the 48 * user an opportunity to enter commandline arguments in a more 49 * user-friendly way. In case the DPTool is started without any 50 * commandline arguments, this class' static method 51 * <code>showArgvDialog</code> will be called. This method will then 52 * open a Frame with input fields whose values will construct a new 53 * Argument vector. 54 * 55 * @author Pawel Kowalski 56 * @version 1.0-beta 57 * 58 * @tbd provide a superclass because the structure of the Jframes is mostly the same. 59 */ 60 public class ArgvDialog extends JFrame { 61 62 //*********************************************************************************/ 63 // static members 64 //*********************************************************************************/ 65 66 private static ArgvDialog s_frame = null; 67 private static final int FRAME_WIDTH = 550; 68 private static final int FRAME_HEIGHT = 325; 69 private static final int ROWS = 8; 70 private static final int COLUMNS = 3; 71 private static final int TXT_FIELD_WIDTH = 30; 72 73 private static final String ARGV_FRAME_NAME = "Dependency Tool"; 74 private static final String PROJECT_ROOT = "Project root"; 75 private static final String PERSPECTIVE_FILE = "Perspective file"; 76 private static final String WINDOWS = "OS: Windows"; 77 private static final String WINDOWS_YES = "Yes"; 78 private static final String WINDOWS_NO = "No"; 79 private static final String PROXY = "Proxy"; 80 private static final String FILTER = "Filter (regexp)"; 81 private static final String BROWSE = "Browse"; 82 private static final String GUI = "Start GUI"; 83 private static final String CANCEL = "Cancel"; 84 85 //*********************************************************************************/ 86 // frame gui members 87 //*********************************************************************************/ 88 89 private JTextField m_projectTextField = null; 90 private JTextField m_perspectiveTextField = null; 91 private JTextField m_proxyTextField = null; 92 private JTextField m_filterTextField = null; 93 94 private JButton m_rootBrowseButton = null; 95 private JButton m_perspectiveBrowseButton = null; 96 private JButton m_guiButton = null; 97 private JButton m_cancelButton = null; 98 99 private JRadioButton m_windowsYesButton = null; 100 private JRadioButton m_windowsNoButton = null; 101 102 private JLabel m_messageLabel = null; 103 private JFileChooser m_fileChooser = null; 104 105 //*********************************************************************************/ 106 // static utility method 107 //*********************************************************************************/ 108 109 private String[] m_argv = null; 110 private boolean m_done = false; 111 112 /*** 113 * A public utility method for using this Class. A user of this 114 * class will usually call only this method. 115 * 116 * @param argv a <code>String[]</code> value 117 * @return a <code>String[]</code> value 118 */ 119 public static String[] showArgvDialog(String[] argv) { 120 121 s_frame = new ArgvDialog(argv); 122 s_frame.setVisible(true); 123 124 synchronized (s_frame) { 125 try { 126 s_frame.wait(); 127 } catch (InterruptedException e) { 128 while (!s_frame.m_done) { 129 // in case of an Exception wait for termination of this frame 130 } 131 } 132 } 133 134 s_frame.dispose(); 135 136 return s_frame.m_argv; 137 } 138 139 //*********************************************************************************/ 140 // constructor 141 //*********************************************************************************/ 142 143 /*** 144 * Creates a new <code>ArgvDialog</code> instance. 145 * 146 * @param argv a <code>String[]</code> value 147 */ 148 public ArgvDialog(String[] argv) { 149 m_argv = argv; 150 151 initFrame(); 152 setContentPane(makeGUI()); 153 initForm(); 154 initListeners(); 155 } 156 157 //*********************************************************************************/ 158 // init methods 159 //*********************************************************************************/ 160 161 /*** 162 * Initialize this JFrame. 163 * 164 * @tbd get the icon through a RessourceManager, not directly. 165 */ 166 private void initFrame() { 167 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 168 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 169 170 setResizable(false); 171 setSize(FRAME_WIDTH, FRAME_HEIGHT); 172 setTitle(ARGV_FRAME_NAME); 173 setIconImage(IconGrabber.getImage("dpticon.png")); 174 175 Toolkit tollkit = Toolkit.getDefaultToolkit(); 176 int screenWidth = tollkit.getScreenSize().width; 177 int screenHeight = tollkit.getScreenSize().height; 178 179 setLocation((screenWidth - FRAME_WIDTH) / 2, 180 (screenHeight - FRAME_HEIGHT) / 2); 181 } 182 183 /*** 184 * Make the GUI. 185 * 186 * @return a <code>Container</code> value 187 */ 188 private Container makeGUI() { 189 190 double size[][] = 191 {{-2.0, -2.0, -1.0, -2.0}, // Columns 192 {-2.0, 30.0, 0.0, 0.0, 30.0, 10.0, 30.0, 10.0, 30.0, 30.0, 10.0, 30.0, 20.0, 40.0}}; // Rows 193 194 JPanel contentPane = new JPanel(); 195 contentPane.setLayout(new TableLayout(size)); 196 contentPane.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); 197 198 JLabel projectLabel = new JLabel(PROJECT_ROOT); 199 JLabel perspectiveLabel = new JLabel(PERSPECTIVE_FILE); 200 JLabel windowsLabel = new JLabel(WINDOWS); 201 JLabel proxyLabel = new JLabel(PROXY); 202 JLabel filterLabel = new JLabel(FILTER); 203 204 m_projectTextField = new JTextField(TXT_FIELD_WIDTH); 205 m_perspectiveTextField = new JTextField(TXT_FIELD_WIDTH); 206 m_proxyTextField = new JTextField(TXT_FIELD_WIDTH); 207 m_filterTextField = new JTextField(TXT_FIELD_WIDTH); 208 209 m_rootBrowseButton = new JButton(BROWSE); 210 m_perspectiveBrowseButton = new JButton(BROWSE); 211 m_cancelButton = new JButton(CANCEL); 212 m_guiButton = new JButton(GUI); 213 214 m_windowsYesButton = new JRadioButton(WINDOWS_YES); 215 m_windowsYesButton.setSelected(true); 216 m_windowsNoButton = new JRadioButton(WINDOWS_NO); 217 ButtonGroup windowsButtonGroup = new ButtonGroup(); 218 windowsButtonGroup.add(m_windowsYesButton); 219 windowsButtonGroup.add(m_windowsNoButton); 220 221 // some gui components 222 // 223 m_fileChooser = new JFileChooser(); 224 m_messageLabel = new JLabel(); 225 m_messageLabel.setForeground(Color.red); 226 m_messageLabel.setFont(new Font("Arial", Font.BOLD, 20)); 227 JPanel panel = null; 228 229 // 1'st row 230 // 231 contentPane.add(projectLabel, "1, 1, R, F"); 232 contentPane.add(m_projectTextField, "2, 1, C, C"); 233 contentPane.add(m_rootBrowseButton, "3, 1, L, C"); 234 235 // 2'nd row 236 // 237 // leave unused 238 // 239 contentPane.add(new JLabel(), "1, 2, R, F"); 240 241 // 3'rd row 242 // 243 // leave unused 244 // 245 contentPane.add(new JLabel(), "1, 3, 3, 3"); 246 247 // 4'th row 248 // 249 contentPane.add(perspectiveLabel, "1, 4, F, F"); 250 contentPane.add(m_perspectiveTextField, "2, 4, C, C"); 251 contentPane.add(m_perspectiveBrowseButton, "3, 4, L, C"); 252 253 // 5'th row 254 // 255 // leave unused 256 // 257 contentPane.add(new JPanel(), "1, 5, R, F"); 258 259 // 6'th row 260 // 261 contentPane.add(windowsLabel, "1, 6, R, F"); 262 panel = new JPanel(); 263 panel.add(m_windowsYesButton); 264 panel.add(m_windowsNoButton); 265 contentPane.add(panel, "2, 6, F, F"); 266 267 // 7'th row 268 // 269 // leave unused 270 // 271 contentPane.add(new JLabel(), "2, 7, F, F"); 272 273 // 8'th row 274 // 275 contentPane.add(proxyLabel, "1, 8, R, F"); 276 contentPane.add(m_proxyTextField, "2, 8, C, C"); 277 contentPane.add(new JLabel(""), "3, 8, C, C"); 278 279 // 9'th row 280 // 281 contentPane.add(filterLabel, "1, 9, R, F"); 282 contentPane.add(m_filterTextField, "2, 9, C, C"); 283 contentPane.add(new JLabel(""), "3, 9, C, C"); 284 285 // 10'th row 286 // 287 // leave unused 288 // 289 contentPane.add(new JLabel(), "2, 10, F, F"); 290 291 // 11'th row 292 // 293 panel = new JPanel(); 294 panel.add(m_guiButton); 295 panel.add(m_cancelButton); 296 contentPane.add(panel, "2, 11, C, C"); 297 298 // 12'th row 299 // 300 // leave unused 301 // 302 contentPane.add(new JLabel(), "2, 12, F, F"); 303 304 // 13'th row 305 // 306 panel = new JPanel(); 307 panel.setBorder(BorderFactory.createEtchedBorder()); 308 panel.add(m_messageLabel); 309 contentPane.add(panel, "1, 13, 3, 13"); 310 311 return contentPane; 312 } 313 314 /*** 315 * Initialize the formular with provided data. 316 */ 317 private void initForm() { 318 try { 319 // setup and parse command line 320 // 321 Configuration config = new Configuration(); 322 CommandLineConfiguration commandLineConfig 323 = new CommandLineConfiguration(DPT_CONST.CMDLINE_SEPARATOR, 324 DPTool.OPTIONS, false); 325 commandLineConfig.parse(m_argv, config); 326 327 // fill project root field 328 // 329 String rootRequest = config.getString(DPT_CONST.SECTION, 330 DPT_CONST.ROOT, 331 DPT_CONST.NOARG); 332 if (!DPT_CONST.NOARG.equals(rootRequest)) { 333 m_projectTextField.setText(rootRequest); 334 } 335 336 // fill filter field 337 // 338 String filterRequest = config.getString(DPT_CONST.SECTION, 339 DPT_CONST.FILTER, 340 DPT_CONST.NOARG); 341 if (!DPT_CONST.NOARG.equals(filterRequest)) { 342 m_filterTextField.setText(filterRequest); 343 } else { 344 m_filterTextField.setText(DPT_CONST.BASIC_FILTER); 345 } 346 347 // file perspective file field 348 // 349 String perspectiveRequest = config.getString(DPT_CONST.SECTION, 350 DPT_CONST.PERSPECTIVE, 351 DPT_CONST.NOARG); 352 if (!DPT_CONST.NOARG.equals(perspectiveRequest)) { 353 m_perspectiveTextField.setText(perspectiveRequest); 354 } 355 356 357 // set used os is windows 358 // 359 String nowindowsRequest = config.getString(DPT_CONST.SECTION, 360 DPT_CONST.NOWIN, 361 DPT_CONST.NOARG); 362 if (DPT_CONST.EMPTY_STRING.equals(nowindowsRequest)) { 363 m_windowsNoButton.setSelected(true); 364 } 365 366 // fill proxy field 367 // 368 String proxyRequest = config.getString(DPT_CONST.SECTION, 369 DPT_CONST.PROXY, 370 DPT_CONST.NOARG); 371 if (!DPT_CONST.NOARG.equals(proxyRequest)) { 372 m_proxyTextField.setText(proxyRequest); 373 } 374 375 } catch (Exception e) { 376 // 377 // user must have entered invalid cmd line args 378 // just don't prefill the form 379 // 380 } 381 } 382 383 /*** 384 * Initialize all Listeners. 385 */ 386 private void initListeners() { 387 m_rootBrowseButton.addActionListener(new ActionListener() { 388 public void actionPerformed(ActionEvent e) { 389 handleRootBrowseButton(); 390 } 391 }); 392 m_perspectiveBrowseButton.addActionListener(new ActionListener() { 393 public void actionPerformed(ActionEvent e) { 394 handlePerspectiveBrowseButton(); 395 } 396 }); 397 m_guiButton.addActionListener(new ActionListener() { 398 public void actionPerformed(ActionEvent e) { 399 handleGuiButton(); 400 } 401 }); 402 m_cancelButton.addActionListener(new ActionListener() { 403 public void actionPerformed(ActionEvent e) { 404 handleCancelButton(); 405 } 406 }); 407 m_projectTextField.addActionListener(new ActionListener() { 408 public void actionPerformed(ActionEvent e) { 409 handleGuiButton(); 410 } 411 }); 412 m_perspectiveTextField.addActionListener(new ActionListener() { 413 public void actionPerformed(ActionEvent e) { 414 handleGuiButton(); 415 } 416 }); 417 m_proxyTextField.addActionListener(new ActionListener() { 418 public void actionPerformed(ActionEvent e) { 419 handleGuiButton(); 420 } 421 }); 422 m_filterTextField.addActionListener(new ActionListener() { 423 public void actionPerformed(ActionEvent e) { 424 handleGuiButton(); 425 } 426 }); 427 } 428 429 //*********************************************************************************/ 430 // handle actions methods 431 //*********************************************************************************/ 432 433 /*** 434 * Handles Browse button next to the root field. 435 */ 436 private void handleRootBrowseButton() { 437 m_fileChooser.resetChoosableFileFilters(); 438 m_fileChooser.setFileFilter(MyFileFilter.getFileFilter("jar")); 439 m_fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 440 441 try { 442 File currentDir = new File(m_projectTextField.getText()); 443 if (currentDir.isDirectory()) { 444 m_fileChooser.setCurrentDirectory(currentDir); 445 } else if (currentDir.getParentFile().isDirectory()) { 446 m_fileChooser.setCurrentDirectory(currentDir.getParentFile()); 447 } 448 } catch (Exception e) { 449 // the intention is to really do nothing here 450 // if the path is null, an Exception will be thrown 451 // what means, that its simply not necessary to set 452 // current dir 453 } 454 455 if (m_fileChooser.showDialog(this, "Select File or Directory") == JFileChooser.APPROVE_OPTION) { 456 File file = m_fileChooser.getSelectedFile(); 457 if (file.exists()) { 458 m_projectTextField.setText(file.getAbsolutePath()); 459 } else if (new File(file.getParent()).exists()) { 460 m_projectTextField.setText(new File(file.getParent()).getAbsolutePath()); 461 } 462 } 463 } 464 465 /*** 466 * Handles Browse Browse next to Perspectives TextField 467 */ 468 private void handlePerspectiveBrowseButton() { 469 m_fileChooser.resetChoosableFileFilters(); 470 m_fileChooser.setFileFilter(MyFileFilter.getFileFilter("xml")); 471 m_fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 472 473 try { 474 File currentDir = new File(m_perspectiveTextField.getText()).getParentFile(); 475 if (currentDir.isDirectory()) { 476 m_fileChooser.setCurrentDirectory(currentDir); 477 } 478 } catch (Exception e) { 479 // the intention is to really do nothing here 480 // if the path is null, an Exception will be thrown 481 // what means, that its simply not necessary to set 482 // current dir 483 } 484 485 if (m_fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 486 m_perspectiveTextField.setText(m_fileChooser.getSelectedFile().toString()); 487 } 488 } 489 490 /*** 491 * Handles GUI-Button Events. 492 */ 493 private void handleGuiButton() { 494 if (validateForGUIMode()) { 495 m_argv = createDPToolArgv(); 496 terminate(); 497 return; 498 } 499 displayGuiModeError(); 500 return; 501 } 502 503 /*** 504 * Handles Cancel-Button Events. 505 */ 506 private void handleCancelButton() { 507 m_argv = null; 508 terminate(); 509 return; 510 } 511 512 /*** 513 * Terminate this frame. 514 */ 515 private synchronized void terminate() { 516 m_done = true; 517 notifyAll(); 518 } 519 520 //*********************************************************************************/ 521 // create new argument vector method 522 //*********************************************************************************/ 523 524 /*** 525 * Create a new argument vector that will be used by the 526 * MainFrame.execute() method to start the Dependency Tool. 527 * 528 * @return a <code>String[]</code> value 529 */ 530 private String[] createDPToolArgv() { 531 String tempString = ""; 532 ArrayList argList = new ArrayList(); 533 534 // project root 535 // 536 argList.add(DPT_CONST.CMDLINE_SEPARATOR + DPT_CONST.ROOT); 537 argList.add(m_projectTextField.getText().trim()); 538 539 // project filter 540 // 541 tempString = m_filterTextField.getText().trim(); 542 if (!DPT_CONST.EMPTY_STRING.equals(tempString)) { 543 argList.add(DPT_CONST.CMDLINE_SEPARATOR + DPT_CONST.FILTER); 544 argList.add(tempString); 545 } 546 547 // perspective file 548 // 549 tempString = m_perspectiveTextField.getText().trim(); 550 if (!DPT_CONST.EMPTY_STRING.equals(tempString)) { 551 argList.add(DPT_CONST.CMDLINE_SEPARATOR + DPT_CONST.PERSPECTIVE); 552 argList.add(tempString); 553 } 554 555 // is os windows? 556 // 557 if (m_windowsNoButton.isSelected()) { 558 argList.add(DPT_CONST.CMDLINE_SEPARATOR + DPT_CONST.NOWIN); 559 } 560 561 // layout proxy 562 // 563 tempString = m_proxyTextField.getText().trim(); 564 if (!DPT_CONST.EMPTY_STRING.equals(tempString)) { 565 argList.add(DPT_CONST.CMDLINE_SEPARATOR + DPT_CONST.PROXY); 566 argList.add(tempString); 567 } 568 569 return (String[])argList.toArray(new String[argList.size()]); 570 } 571 572 //*********************************************************************************/ 573 // validate methods 574 //*********************************************************************************/ 575 576 /*** 577 * Validate inputs for the GUI startup. This method assures that 578 * the input will be sufficient to construc an argument vector for 579 * starting the Dependency Tool, else an error message will be 580 * shown and the user requested to provide the neccessary 581 * information. 582 * 583 * @return a <code>boolean</code> value 584 */ 585 private boolean validateForGUIMode() { 586 return !DPT_CONST.EMPTY_STRING.equals(m_projectTextField.getText().trim()); 587 } 588 589 //*********************************************************************************/ 590 // display error methods 591 //*********************************************************************************/ 592 593 /*** 594 * Display an error message. 595 */ 596 private void displayGuiModeError() { 597 m_messageLabel.setText("Specify project root!"); 598 } 599 600 //*********************************************************************************/ 601 // overriden methods 602 //*********************************************************************************/ 603 604 /*** 605 * Overriden <code>processWindowEvent</code> method. 606 * 607 * @param e a <code>WindowEvent</code> value 608 */ 609 protected void processWindowEvent(WindowEvent e) { 610 super.processWindowEvent(e); 611 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 612 System.exit(0); 613 } 614 } 615 }

This page was automatically generated by Maven