1 23 package org.enhydra.kelp.common.deployer; 24 25 import org.enhydra.kelp.common.PathUtil; 27 import org.enhydra.kelp.common.event.WriteListener; 28 import org.enhydra.kelp.common.node.OtterProject; 29 30 import org.enhydra.tool.archive.ArchiveException; 32 import org.enhydra.tool.archive.JarPlan; 33 import org.enhydra.tool.common.DataValidationException; 34 import org.enhydra.tool.common.PathHandle; 35 36 import java.awt.*; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.ActionListener ; 40 import java.io.PrintWriter ; 41 import java.util.Arrays ; 42 import javax.swing.*; 43 import javax.swing.border.Border ; 44 import javax.swing.border.TitledBorder ; 45 import java.beans.*; 46 47 public class ArchivePanel extends JPanel implements Instructor, DeployStep { 49 private GridBagLayout layoutMain; 50 private JScrollPane scroll; 51 private ArchiveTablePanel archiveTable; 52 private JPanel panelButton; 53 private JButton buttonAdd; 54 private JButton buttonRemove; 55 private GridBagLayout layoutButton; 56 private JButton buttonUp; 57 private JButton buttonDown; 58 private LocalButtonListener buttonListener = null; 59 private boolean ran = false; 60 private JButton buttonEdit; 61 62 public ArchivePanel() { 63 try { 64 jbInit(); 65 pmInit(); 66 } catch (Exception ex) { 67 ex.printStackTrace(); 68 } 69 } 70 71 public String getTab() { 73 return "Archive"; 74 } 75 76 public String getTitle() { 78 return "Deployable archives"; 79 } 80 81 public String getInstructions() { 83 return "The deployer can recreate archives that you can auto-deploy " 84 + "to a running Enhydra application server."; 85 } 86 87 public void refresh() { 89 90 } 92 93 public void clearAll() { 95 archiveTable.clearAll(); 96 buttonAdd.removeActionListener(buttonListener); 97 buttonRemove.removeActionListener(buttonListener); 98 buttonUp.removeActionListener(buttonListener); 99 buttonDown.removeActionListener(buttonListener); 100 removeAll(); 101 buttonListener = null; 102 } 103 104 public boolean isDataValid() { 106 return DeployStepUtil.isDataValid(this); 107 } 108 109 public boolean isDataEqual(OtterProject project) { 111 boolean equal = true; 112 JarPlan[] projectPlans = new JarPlan[0]; 113 JarPlan[] panelPlans = new JarPlan[0]; 114 115 projectPlans = project.getArchivePlans(false); 116 panelPlans = archiveTable.getPlans(); 117 if (!Arrays.equals(panelPlans, projectPlans)) { 118 equal = false; 119 } 120 return equal; 121 } 122 123 public void validateData() throws DataValidationException { 125 JarPlan[] plans = new JarPlan[0]; 126 PathHandle path = null; 127 128 plans = archiveTable.getPlans(); 129 for (int i = 0; i < plans.length; i++) { 130 path = PathHandle.createPathHandle(plans[i].getArchivePath()); 131 if (path.isDirectory()) { 132 throw new DataValidationException("Invalid archive: " 133 + path.getPath()); 134 } 135 } 136 } 137 138 public boolean isBuilt(OtterProject project) { 140 ran = DeployStepUtil.isBuilt(this, ran, project); 141 return ran; 142 } 143 144 public boolean isSelectable(OtterProject project, DeployStep[] steps) { 146 boolean able = false; 147 boolean generalBuilt = false; 148 boolean inputBuilt = false; 149 boolean contentBuilt = false; 150 151 for (int i = 0; i < steps.length; i++) { 152 if (steps[i] instanceof GeneralPanel) { 153 generalBuilt = steps[i].isBuilt(project); 154 } else if (steps[i] instanceof InputPanel) { 155 inputBuilt = steps[i].isBuilt(project); 156 } else if (steps[i] instanceof ContentPanel) { 157 contentBuilt = steps[i].isBuilt(project); 158 } 159 } 160 able = generalBuilt && inputBuilt && contentBuilt; 161 return able; 162 } 163 164 public void build(OtterProject project, 166 WriteListener listener) throws DataValidationException { 167 ArchiveBuilder builder = null; 168 169 if (!isBuilt(project)) { 170 write(project); 171 builder = new ArchiveBuilder(listener); 172 builder.setEcho(true); 173 builder.setProject(project); 174 builder.buildInCurrentThread(); 175 ran = true; 176 } 177 } 178 179 public void read(OtterProject project) { 181 PathHandle ph = null; 182 JarPlan[] plans = new JarPlan[0]; 183 184 plans = project.getArchivePlans(false); 186 archiveTable.setPlans(plans); 187 ph = 188 PathHandle.createPathHandle(PathUtil.getDefaultDeployArchivePath(project)); 189 archiveTable.setDefWorkingRoot(ph.getParent().getPath()); 190 } 191 192 public void write(OtterProject project) throws DataValidationException { 194 JarPlan[] plans = new JarPlan[0]; 195 196 validateData(); 197 if (!isDataEqual(project)) { 198 plans = archiveTable.getPlans(); 199 project.setArchivePlans(plans); 200 } 201 } 202 203 private void pmInit() { 206 buttonListener = new LocalButtonListener(); 207 buttonAdd.addActionListener(buttonListener); 208 buttonEdit.addActionListener(buttonListener); 209 buttonRemove.addActionListener(buttonListener); 210 buttonUp.addActionListener(buttonListener); 211 buttonDown.addActionListener(buttonListener); 212 } 213 214 private void jbInit() throws Exception { 215 scroll = (JScrollPane) Beans.instantiate(getClass().getClassLoader(), 216 JScrollPane.class.getName()); 217 archiveTable = 218 (ArchiveTablePanel) Beans.instantiate(getClass().getClassLoader(), 219 ArchiveTablePanel.class.getName()); 220 panelButton = (JPanel) Beans.instantiate(getClass().getClassLoader(), 221 JPanel.class.getName()); 222 buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(), 223 JButton.class.getName()); 224 buttonRemove = 225 (JButton) Beans.instantiate(getClass().getClassLoader(), 226 JButton.class.getName()); 227 layoutButton = 228 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 229 GridBagLayout.class.getName()); 230 buttonUp = (JButton) Beans.instantiate(getClass().getClassLoader(), 231 JButton.class.getName()); 232 buttonDown = (JButton) Beans.instantiate(getClass().getClassLoader(), 233 JButton.class.getName()); 234 layoutMain = 235 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 236 GridBagLayout.class.getName()); 237 buttonEdit = (JButton) Beans.instantiate(getClass().getClassLoader(), 238 JButton.class.getName()); 239 buttonAdd.setMaximumSize(new Dimension(100, 27)); 240 buttonAdd.setMinimumSize(new Dimension(100, 27)); 241 buttonAdd.setPreferredSize(new Dimension(100, 27)); 242 buttonAdd.setText("Add..."); 243 buttonRemove.setMaximumSize(new Dimension(100, 27)); 244 buttonRemove.setMinimumSize(new Dimension(100, 27)); 245 buttonRemove.setPreferredSize(new Dimension(100, 27)); 246 buttonRemove.setText("Remove"); 247 panelButton.setLayout(layoutButton); 248 buttonUp.setMaximumSize(new Dimension(100, 27)); 249 buttonUp.setMinimumSize(new Dimension(100, 27)); 250 buttonUp.setPreferredSize(new Dimension(100, 27)); 251 buttonUp.setText("Move Up"); 252 buttonDown.setMaximumSize(new Dimension(100, 27)); 253 buttonDown.setMinimumSize(new Dimension(100, 27)); 254 buttonDown.setPreferredSize(new Dimension(100, 27)); 255 buttonDown.setText("Move Down"); 256 buttonEdit.setMaximumSize(new Dimension(100, 27)); 257 buttonEdit.setMinimumSize(new Dimension(100, 27)); 258 buttonEdit.setPreferredSize(new Dimension(100, 27)); 259 buttonEdit.setText("Edit..."); 260 panelButton.add(buttonAdd, 261 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 262 GridBagConstraints.CENTER, 263 GridBagConstraints.NONE, 264 new Insets(20, 5, 5, 5), 0, 265 0)); 266 panelButton.add(buttonRemove, 267 new GridBagConstraints(0, 2, 1, 1, 0.1, 0.0, 268 GridBagConstraints.CENTER, 269 GridBagConstraints.NONE, 270 new Insets(5, 5, 5, 5), 0, 0)); 271 panelButton.add(buttonUp, 272 new GridBagConstraints(0, 3, 1, 1, 0.1, 0.0, 273 GridBagConstraints.CENTER, 274 GridBagConstraints.NONE, 275 new Insets(5, 5, 5, 5), 0, 0)); 276 panelButton.add(buttonDown, 277 new GridBagConstraints(0, 4, 1, 1, 0.1, 0.0, 278 GridBagConstraints.CENTER, 279 GridBagConstraints.NONE, 280 new Insets(5, 5, 20, 5), 0, 281 0)); 282 panelButton.add(buttonEdit, 283 new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0, 284 GridBagConstraints.CENTER, 285 GridBagConstraints.NONE, 286 new Insets(5, 5, 5, 5), 0, 0)); 287 scroll.getViewport().add(archiveTable, null); 288 this.setLayout(layoutMain); 289 this.add(scroll, 290 new GridBagConstraints(0, 0, 1, 1, 0.8, 0.0, 291 GridBagConstraints.CENTER, 292 GridBagConstraints.BOTH, 293 new Insets(5, 5, 5, 5), 0, 0)); 294 this.add(panelButton, 295 new GridBagConstraints(1, 0, 1, 1, 0.1, 0.0, 296 GridBagConstraints.CENTER, 297 GridBagConstraints.BOTH, 298 new Insets(5, 5, 5, 5), 0, 0)); 299 } 300 301 private class LocalButtonListener implements ActionListener { 303 public void actionPerformed(ActionEvent event) { 304 Object source = event.getSource(); 305 306 try { 307 if (source == buttonAdd) { 308 archiveTable.addRow(); 309 } else if (source == buttonEdit) { 310 archiveTable.editRow(); 311 } else if (source == buttonRemove) { 312 archiveTable.removeSelection(); 313 } else if (source == buttonDown) { 314 archiveTable.moveSelectionDown(); 315 } else if (source == buttonUp) { 316 archiveTable.moveSelectionUp(); 317 } 318 } catch (ArchiveException e) { 319 JOptionPane.showMessageDialog(getTopLevelAncestor(), 320 e.getMessage()); 321 } 322 } 323 324 } 325 } 326 | Popular Tags |