1 2 24 package org.enhydra.tool.archive.wizard; 25 26 import org.enhydra.tool.archive.ArchiveException; 28 import org.enhydra.tool.archive.Descriptor; 29 import org.enhydra.tool.common.PathHandle; 30 import org.enhydra.tool.common.ExtensionFilter; 31 import org.enhydra.tool.common.FilenameFilter; 32 import org.enhydra.tool.common.SwingUtil; 33 34 import java.awt.*; 36 import java.awt.event.*; 37 import java.io.File ; 38 import javax.swing.*; 39 import javax.swing.event.ChangeEvent ; 40 import javax.swing.event.ChangeListener ; 41 import javax.swing.border.TitledBorder ; 42 import javax.swing.table.DefaultTableCellRenderer ; 43 import java.beans.Beans ; 44 45 public class FilePanel extends RefPanel { 47 private JPanel panelDescript = null; 48 private GridBagLayout layoutMain = null; 49 private JPanel panelFile = null; 50 private JTextField textFile = null; 51 private JButton buttonFile = null; 52 private GridBagLayout layoutFile = null; 53 private GridBagLayout layoutDescript = null; 54 private JButton buttonDD = null; 55 private LocalButtonListener buttonListener = null; 56 private JScrollPane scroll = null; 57 private JTable tableDD = null; 58 private String extension = "jar"; 59 private String title = "Archive"; 60 private String [] defaultDD = new String [0]; 61 62 public FilePanel() { 63 try { 64 jbInit(); 65 pmInit(); 66 } catch (Exception ex) { 67 ex.printStackTrace(); 68 } 69 } 70 71 protected void setExtension(String s) { 72 extension = s; 73 } 74 75 protected String getExtension() { 76 return extension; 77 } 78 79 protected void setTitle(String s) { 80 title = s; 81 } 82 83 protected String getTitle() { 84 return title; 85 } 86 87 protected String [] getDefaultDD() { 88 return defaultDD; 89 } 90 91 protected void setDefaultDD(String [] dd) { 92 defaultDD = dd; 93 } 94 95 protected void validatePlan() throws ArchiveException { 96 int count = -1; 97 98 if (getArchivePath().trim().length() == 0) { 99 throw new ArchiveException("Archive file name required"); 100 } else { 101 PathHandle path = null; 102 103 path = PathHandle.createPathHandle(getArchivePath()); 104 if (!path.hasExtension(getExtension())) { 105 throw new ArchiveException("Invalid archive extension: " 106 + path.getExtension() 107 + "\n Expecting: " 108 + getExtension()); 109 } 110 } 111 count = getDDTableModel().getRowCount(); 112 for (int i = 0; i < count; i++) { 113 Descriptor row = null; 114 115 row = getDDTableModel().getDescriptor(i); 116 if (row.isRequired()) { 117 if (row.getPath().trim().length() == 0) { 118 throw new ArchiveException(row.getType() 119 + " descriptor required"); 120 } else { 121 File file = null; 122 123 file = new File (row.getPath()); 124 if (!file.isFile()) { 125 throw new ArchiveException("Invalid descriptor: " 126 + file.getAbsolutePath()); 127 } 128 } 129 } 130 } 131 } 132 133 protected String getArchivePath() { 134 return textFile.getText(); 135 } 136 137 protected void setArchivePath(String path) { 138 PathHandle handle = PathHandle.createPathHandle(path); 139 140 textFile.setText(handle.getPath()); 141 if (getWizard() != null) { 142 for (int i = 0; i < 3; i++) { 143 if (handle.getParent() == null) { 144 break; 145 } else { 146 handle = handle.getParent(); 147 } 148 } 149 if (handle.isDirectory()) { 150 setWorkingRoot(handle.getPath()); 151 } 152 } 153 } 154 155 protected DDTableModel getDDTableModel() { 156 return (DDTableModel) tableDD.getModel(); 157 } 158 159 protected JTable getDDTable() { 160 return tableDD; 161 } 162 163 protected void preparePanel() { 164 panelFile.setBorder(new TitledBorder (BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)), 165 getTitle())); 166 setArchivePath(getArchivePath()); 167 if (!getDDTable().isVisible()) { 168 remove(panelDescript); 169 doLayout(); 170 } 171 } 172 173 private void pmInit() { 174 CheckCellRenderer checkRenderer = null; 175 TextCellRenderer textRenderer = null; 176 177 checkRenderer = new CheckCellRenderer(); 178 textRenderer = new TextCellRenderer(); 179 buttonListener = new LocalButtonListener(); 180 buttonFile.addActionListener(buttonListener); 181 buttonDD.addActionListener(buttonListener); 182 183 tableDD.setModel(new DDTableModel()); 185 tableDD.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); 186 tableDD.getColumnModel().getColumn(0).setCellRenderer(checkRenderer); 187 tableDD.getColumnModel().getColumn(1).setCellRenderer(textRenderer); 188 tableDD.getColumnModel().getColumn(2).setCellRenderer(textRenderer); 189 tableDD.getColumnModel().getColumn(0).setPreferredWidth(50); 190 tableDD.getColumnModel().getColumn(1).setPreferredWidth(50); 191 tableDD.getColumnModel().getColumn(2).setPreferredWidth(100); 192 } 193 194 private void jbInit() throws Exception { 196 panelDescript = 197 (JPanel) Beans.instantiate(getClass().getClassLoader(), 198 JPanel.class.getName()); 199 layoutMain = 200 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 201 GridBagLayout.class.getName()); 202 panelFile = (JPanel) Beans.instantiate(getClass().getClassLoader(), 203 JPanel.class.getName()); 204 textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(), 205 JTextField.class.getName()); 206 buttonFile = (JButton) Beans.instantiate(getClass().getClassLoader(), 207 JButton.class.getName()); 208 layoutFile = 209 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 210 GridBagLayout.class.getName()); 211 layoutDescript = 212 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 213 GridBagLayout.class.getName()); 214 buttonDD = (JButton) Beans.instantiate(getClass().getClassLoader(), 215 JButton.class.getName()); 216 scroll = (JScrollPane) Beans.instantiate(getClass().getClassLoader(), 217 JScrollPane.class.getName()); 218 tableDD = (JTable) Beans.instantiate(getClass().getClassLoader(), 219 JTable.class.getName()); 220 panelFile.setBorder(new TitledBorder (BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)), 221 getTitle())); 222 panelFile.setLayout(layoutFile); 223 panelDescript.setBorder(new TitledBorder (BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)), 224 "Deployment Descriptors")); 225 panelDescript.setLayout(layoutDescript); 226 textFile.setEnabled(false); 227 textFile.setText("ARCHIVE FILE"); 228 buttonFile.setMargin(new Insets(2, 6, 2, 6)); 229 buttonFile.setText("..."); 230 buttonDD.setMargin(new Insets(2, 6, 2, 6)); 231 buttonDD.setText("..."); 232 panelDescript.add(buttonDD, 233 new GridBagConstraints(3, 0, 1, 1, 0.1, 0.1, 234 GridBagConstraints.CENTER, 235 GridBagConstraints.NONE, 236 new Insets(5, 0, 5, 1), 0, 237 0)); 238 panelDescript.add(scroll, 239 new GridBagConstraints(2, 0, 1, 1, 0.6, 0.6, 240 GridBagConstraints.CENTER, 241 GridBagConstraints.BOTH, 242 new Insets(5, 5, 5, 5), 100, 243 60)); 244 scroll.getViewport().add(tableDD, null); 245 panelFile.add(textFile, 246 new GridBagConstraints(0, 0, 1, 1, 0.6, 0.6, 247 GridBagConstraints.WEST, 248 GridBagConstraints.HORIZONTAL, 249 new Insets(2, 10, 2, 2), 0, 0)); 250 panelFile.add(buttonFile, 251 new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1, 252 GridBagConstraints.CENTER, 253 GridBagConstraints.NONE, 254 new Insets(1, 1, 1, 1), 0, 0)); 255 this.setLayout(layoutMain); 256 this.add(panelDescript, 257 new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1, 258 GridBagConstraints.CENTER, 259 GridBagConstraints.BOTH, 260 new Insets(4, 5, 4, 5), 0, 0)); 261 this.add(panelFile, 262 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1, 263 GridBagConstraints.CENTER, 264 GridBagConstraints.BOTH, 265 new Insets(4, 5, 4, 5), 0, 0)); 266 } 267 268 private void chooseArchive() { 269 ExtensionFilter filter = null; 270 File choice = null; 271 PathHandle path = null; 272 273 filter = new ExtensionFilter(); 274 filter.setDescriptionTitle(getTitle()); 275 filter.addExtension(getExtension()); 276 choice = SwingUtil.getFileChoice(this, getArchivePath(), filter, 277 "Select or enter new " 278 + getExtension() + " file name"); 279 if (choice == null) { 280 } else { 282 path = PathHandle.createPathHandle(choice); 283 if (path.getExtension().length() == 0) { 284 path.setExtension(getExtension()); 285 } 286 setArchivePath(path.getPath()); 287 if ((getWizard() != null) && path.isFile()) { 288 try { 289 getWizard().readArchive(path.getPath()); 290 } catch (ArchiveException e) { 291 e.printStackTrace(System.err); 292 } 293 } 294 } 295 } 296 297 private void chooseDD() { 298 int index = -1; 299 DDTableModel model = null; 300 Descriptor row = null; 301 File choice = null; 302 FilenameFilter filter = null; 303 PathHandle path = null; 304 305 index = tableDD.getSelectedRow(); 307 if (index > -1) { 308 model = (DDTableModel) tableDD.getModel(); 309 row = model.getDescriptor(index); 310 filter = new FilenameFilter(); 311 filter.setIncludeName(row.getType() + ".xml"); 312 filter.setDescriptionTitle("Deployment Descriptor"); 313 if (row.getPath().trim().length() == 0 314 && getDefaultDD().length > 0) { 315 path = 316 PathHandle.createPathHandle(getWorkingRoot() 317 + File.separator 318 + getDefaultDD()[0]); 319 if (!path.isFile()) { 320 path = 321 PathHandle.createPathHandle(getWorkingRoot()); 322 } 323 } else { 324 path = PathHandle.createPathHandle(row.getPath()); 325 } 326 choice = SwingUtil.getFileChoice(this, path.getPath(), filter, 327 "Select deployment descriptor"); 328 } 329 if (choice == null) { 330 331 } else if (choice.isFile()) { 333 row.setPath(choice.getAbsolutePath()); 334 model.setDescriptor(index, row); 335 } 336 } 337 338 private class LocalButtonListener implements ActionListener { 339 public void actionPerformed(ActionEvent e) { 340 Object source = e.getSource(); 341 342 if (source == buttonFile) { 343 chooseArchive(); 344 } else if (source == buttonDD) { 345 chooseDD(); 346 } 347 } 348 349 } 350 private class CheckCellRenderer extends DefaultTableCellRenderer { 351 public Component getTableCellRendererComponent(JTable table, 352 Object value, boolean isSelected, boolean hasFocus, int row, 353 int column) { 354 Component comp = null; 355 JCheckBox check = null; 356 357 comp = super.getTableCellRendererComponent(table, value, 358 isSelected, hasFocus, 359 row, column); 360 check = new JCheckBox(); 361 check.setBackground(comp.getBackground()); 362 check.setForeground(comp.getForeground()); 363 if (value instanceof Boolean ) { 364 Boolean b = (Boolean ) value; 365 366 check.setSelected(b.booleanValue()); 367 } 368 check.setHorizontalAlignment(SwingConstants.CENTER); 369 return check; 370 } 371 372 } 373 private class TextCellRenderer extends DefaultTableCellRenderer { 374 public Component getTableCellRendererComponent(JTable table, 375 Object value, boolean isSelected, boolean hasFocus, int row, 376 int column) { 377 Component comp = null; 378 379 comp = super.getTableCellRendererComponent(table, value, 380 isSelected, false, 381 row, column); 382 if (comp instanceof JLabel) { 383 JLabel jlab = (JLabel) comp; 384 385 if (column == 1) { 386 jlab.setHorizontalAlignment(SwingConstants.CENTER); 387 } else { 388 jlab.setToolTipText(jlab.getText()); 389 } 390 } 391 return comp; 392 } 393 394 } 395 396 397 398 } 399 | Popular Tags |