1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.wizards; 21 22 import org.openide.WizardDescriptor; 23 import org.openide.WizardValidationException; 24 import org.openide.ErrorManager; 25 import org.openide.util.HelpCtx; 26 27 import javax.swing.*; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import java.util.List ; 31 import java.util.LinkedList ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.awt.*; 35 36 45 public abstract class AbstractStep implements WizardDescriptor.ValidatingPanel { 46 47 private List listeners = new LinkedList (); 48 private boolean valid; 49 private JComponent panel; 50 private volatile boolean underConstruction; 51 private String errorMessage; 52 private boolean applyStandaloneLayout; 53 54 57 public void applyStandaloneLayout() { 58 applyStandaloneLayout = true; 59 } 60 61 65 public final synchronized Component getComponent() { 66 if (panel == null) { 67 try { 68 underConstruction = true; 69 panel = createComponent(); 70 HelpCtx.setHelpIDString(panel, getClass().getName()); 71 if (applyStandaloneLayout == false) { 72 JTextArea template = new JTextArea(); 73 template.setColumns(60); 74 template.setRows(25); 75 panel.setPreferredSize(template.getPreferredSize()); 76 } 77 } catch (RuntimeException ex) { 78 ErrorManager.getDefault().notify(ex); 79 } finally { 80 assert panel != null; 81 underConstruction = false; 82 fireChange(); 83 } 84 } 85 return panel; 86 } 87 88 91 protected abstract JComponent createComponent(); 92 93 public HelpCtx getHelp() { 94 return null; 95 } 96 97 public void readSettings(Object settings) { 98 } 99 100 public void storeSettings(Object settings) { 101 } 102 103 protected final void valid() { 104 setValid(true, null); 105 } 106 107 111 protected final void valid(String extErrorMessage) { 112 setValid(true, extErrorMessage); 113 } 114 115 protected final void invalid(String message) { 116 setValid(false, message); 117 } 118 119 public final boolean isValid() { 120 return valid; 121 } 122 123 public final String getErrorMessage() { 124 return errorMessage; 125 } 126 127 public final void validate () throws WizardValidationException { 129 validateBeforeNext(); 130 if (isValid() == false || errorMessage != null) { 131 throw new WizardValidationException ( 132 panel, 133 errorMessage, 134 errorMessage 135 ); 136 } 137 } 138 139 143 protected abstract void validateBeforeNext(); 144 145 public final void addChangeListener(ChangeListener l) { 146 synchronized(listeners) { 147 listeners.add(l); 148 } 149 } 150 151 public final void removeChangeListener(ChangeListener l) { 152 synchronized(listeners) { 153 listeners.remove(l); 154 } 155 } 156 157 private void setValid(boolean valid, String errorMessage) { 158 boolean fire = AbstractStep.this.valid != valid; 159 fire |= errorMessage != null && (errorMessage.equals(this.errorMessage) == false); 160 AbstractStep.this.valid = valid; 161 this.errorMessage = errorMessage; 162 if (fire) { 163 fireChange(); 164 } 165 } 166 167 private void fireChange() { 168 if (underConstruction) return; 169 List clone; 170 synchronized(listeners) { 171 clone = new ArrayList (listeners); 172 } 173 Iterator it = clone.iterator(); 174 ChangeEvent event = new ChangeEvent (this); 175 while (it.hasNext()) { 176 ChangeListener listener = (ChangeListener ) it.next(); 177 listener.stateChanged(event); 178 } 179 } 180 181 } 182 | Popular Tags |