1 19 20 package org.netbeans.modules.javawebstart.ui.customizer; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Set ; 30 31 import javax.swing.ComboBoxModel ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.JToggleButton ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.DefaultStyledDocument ; 36 import javax.swing.text.Document ; 37 import javax.swing.text.PlainDocument ; 38 import javax.swing.text.StyledDocument ; 39 40 import org.netbeans.api.project.Project; 41 import org.netbeans.api.project.ProjectManager; 42 import org.netbeans.modules.java.j2seproject.api.J2SEPropertyEvaluator; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 45 import org.netbeans.spi.project.support.ant.ui.StoreGroup; 46 import org.openide.filesystems.FileLock; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileSystem; 49 import org.openide.filesystems.FileUtil; 50 import org.openide.util.Lookup; 51 import org.openide.util.Mutex; 52 import org.openide.util.MutexException; 53 import org.openide.util.NbBundle; 54 55 59 public class JWSProjectProperties { 60 61 public static final String JNLP_ENABLED = "jnlp.enabled"; 62 public static final String JNLP_ICON = "jnlp.icon"; 64 public static final String JNLP_OFFLINE = "jnlp.offline-allowed"; 65 public static final String JNLP_CBASE_TYPE = "jnlp.codebase.type"; 66 public static final String JNLP_CBASE_USER = "jnlp.codebase.user"; 67 public static final String JNLP_CBASE_URL = "jnlp.codebase.url"; 68 69 public static final String JNLP_SPEC = "jnlp.spec"; 70 public static final String JNLP_INIT_HEAP = "jnlp.initial-heap-size"; 71 public static final String JNLP_MAX_HEAP = "jnlp.max-heap-size"; 72 73 public static final String JNLP_SIGN_ENABLED = "jnlp.signing.enabled"; 74 75 public static final String CB_TYPE_LOCAL = "local"; 76 public static final String CB_TYPE_WEB = "web"; 77 public static final String CB_TYPE_USER = "user"; 78 79 public static final String CB_URL_WEB = "$$codebase"; 80 81 public static final String CB_URL_WEB_PROP_VALUE = "$$$$codebase"; 83 84 private StoreGroup jnlpPropGroup = new StoreGroup(); 85 86 private J2SEPropertyEvaluator j2sePropEval; 87 private PropertyEvaluator evaluator; 88 private Project j2seProject; 89 90 JToggleButton.ToggleButtonModel enabledModel; 92 JToggleButton.ToggleButtonModel allowOfflineModel; 93 ComboBoxModel codebaseModel; 94 95 Document iconDocument; 97 Document codebaseURLDocument; 98 99 100 public JWSProjectProperties(Lookup context) { 101 102 j2seProject = (Project) context.lookup(Project.class); 103 if (j2seProject != null) { 104 j2sePropEval = (J2SEPropertyEvaluator) j2seProject.getLookup().lookup(J2SEPropertyEvaluator.class); 105 } else { 106 } 108 109 evaluator = j2sePropEval.evaluator(); 110 111 enabledModel = jnlpPropGroup.createToggleButtonModel(evaluator, JNLP_ENABLED); 112 allowOfflineModel = jnlpPropGroup.createToggleButtonModel(evaluator, JNLP_OFFLINE); 113 iconDocument = jnlpPropGroup.createStringDocument(evaluator, JNLP_ICON); 114 115 codebaseModel = new CodebaseComboBoxModel(); 116 codebaseURLDocument = createCBTextFieldDocument(); 117 118 } 119 120 private void storeRest(EditableProperties editableProps) { 121 String selItem = ((CodebaseComboBoxModel) codebaseModel).getSelectedCodebaseItem(); 122 String propName = null; 123 String propValue = null; 124 if (CB_TYPE_USER.equals(selItem)) { 125 propName = JNLP_CBASE_USER; 126 try { 127 propValue = codebaseURLDocument.getText(0, codebaseURLDocument.getLength()); 128 } catch (BadLocationException ex) { 129 return; 132 } 133 } else if (CB_TYPE_LOCAL.equals(selItem)) { 134 propName = JNLP_CBASE_URL; 135 propValue = getProjectDistDir(); 136 } else if (CB_TYPE_WEB.equals(selItem)) { 137 propName = JNLP_CBASE_URL; 138 propValue = CB_URL_WEB_PROP_VALUE; 139 } 140 if (propName == null || propValue == null) { 141 return; 142 } else { 143 editableProps.setProperty(JNLP_CBASE_TYPE, selItem); 144 editableProps.setProperty(propName, propValue); 145 } 146 } 147 148 public void store() throws IOException { 149 150 final EditableProperties ep = new EditableProperties(true); 151 final FileObject projPropsFO = j2seProject.getProjectDirectory().getFileObject("nbproject/project.properties"); 152 153 try { 154 final InputStream is = projPropsFO.getInputStream(); 155 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 156 public Void run() throws Exception { 157 try { 158 ep.load(is); 159 } finally { 160 if (is != null) { 161 is.close(); 162 } 163 } 164 jnlpPropGroup.store(ep); 165 storeRest(ep); 166 OutputStream os = null; 167 FileLock lock = null; 168 try { 169 lock = projPropsFO.lock(); 170 os = projPropsFO.getOutputStream(lock); 171 ep.store(os); 172 } finally { 173 if (lock != null) { 174 lock.releaseLock(); 175 } 176 if (os != null) { 177 os.close(); 178 } 179 } 180 return null; 181 } 182 }); 183 } catch (MutexException mux) { 184 throw (IOException ) mux.getException(); 185 } 186 187 } 188 189 private Document createCBTextFieldDocument() { 190 Document doc = new PlainDocument (); 191 String valueURL = evaluator.getProperty(JNLP_CBASE_USER); 192 String valueType = evaluator.getProperty(JNLP_CBASE_TYPE); 193 String docString = ""; 194 if (CB_TYPE_LOCAL.equals(valueType)) { 195 docString = getProjectDistDir(); 196 } else if (CB_TYPE_WEB.equals(valueType)) { 197 docString = CB_URL_WEB; 198 } else if (CB_TYPE_USER.equals(valueType)) { 199 docString = getCodebaseLocation(); 200 } 201 try { 202 doc.insertString(0, docString, null); 203 } catch (BadLocationException ex) { 204 } 207 return doc; 208 } 209 222 public String getCodebaseLocation() { 223 return evaluator.getProperty(JNLP_CBASE_USER); 224 } 225 226 public String getProjectDistDir() { 227 File distDir = new File (FileUtil.toFile(j2seProject.getProjectDirectory()), evaluator.getProperty("dist.dir")); 228 return distDir.toURI().toString(); 229 } 230 231 public String getProperty(String propName) { 233 return evaluator.getProperty(propName); 234 } 235 236 238 public class CodebaseComboBoxModel extends DefaultComboBoxModel { 239 240 String localLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_Local"); 241 String webLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_Web"); 242 String userLabel = NbBundle.getBundle(JWSProjectProperties.class).getString("LBL_CB_Combo_User"); 243 Object visItems[] = new Object [] { localLabel, webLabel, userLabel }; 244 String cbItems[] = new String [] { CB_TYPE_LOCAL, CB_TYPE_WEB, CB_TYPE_USER }; 245 246 public CodebaseComboBoxModel() { 247 super(); 248 addElement(visItems[0]); 249 addElement(visItems[1]); 250 addElement(visItems[2]); 251 String propValue = evaluator.getProperty(JNLP_CBASE_TYPE); 252 if (cbItems[2].equals(propValue)) { 253 setSelectedItem(visItems[2]); 254 } else if (cbItems[1].equals(propValue)) { 255 setSelectedItem(visItems[1]); 256 } else { 257 setSelectedItem(visItems[0]); 258 } 259 } 260 261 public String getSelectedCodebaseItem() { 262 return cbItems[getIndexOf(getSelectedItem())]; 263 } 264 265 } 266 267 } 268 | Popular Tags |