PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/php.project/src/org/netbeans/modules/php/project/ui/customizer/RunAsInternalServer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 331 lines | 240 code | 35 blank | 56 comment | 11 complexity | 475507ef716512999e8ddb3fc618ba8f MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.project.ui.customizer;
  43. import java.awt.event.ActionEvent;
  44. import java.awt.event.ActionListener;
  45. import java.io.File;
  46. import java.io.FileNotFoundException;
  47. import javax.swing.GroupLayout;
  48. import javax.swing.GroupLayout.Alignment;
  49. import javax.swing.JButton;
  50. import javax.swing.JComboBox;
  51. import javax.swing.JLabel;
  52. import javax.swing.JTextField;
  53. import javax.swing.LayoutStyle.ComponentPlacement;
  54. import javax.swing.event.DocumentListener;
  55. import org.netbeans.modules.php.project.PhpProject;
  56. import org.netbeans.modules.php.project.PhpVisibilityQuery;
  57. import org.netbeans.modules.php.project.ProjectPropertiesSupport;
  58. import org.netbeans.modules.php.project.connections.ConfigManager;
  59. import org.netbeans.modules.php.project.runconfigs.RunConfigInternal;
  60. import org.netbeans.modules.php.project.runconfigs.validation.RunConfigInternalValidator;
  61. import org.netbeans.modules.php.project.ui.Utils;
  62. import org.netbeans.modules.php.project.ui.customizer.PhpProjectProperties.RunAsType;
  63. import org.netbeans.spi.project.ui.support.ProjectCustomizer;
  64. import org.openide.awt.Mnemonics;
  65. import org.openide.filesystems.FileUtil;
  66. import org.openide.util.Exceptions;
  67. import org.openide.util.NbBundle;
  68. /**
  69. * Configuration panel for internal web server.
  70. */
  71. public class RunAsInternalServer extends RunAsPanel.InsidePanel {
  72. private static final long serialVersionUID = -83545468177742547L;
  73. private final PhpProjectProperties properties;
  74. private final PhpProject project;
  75. private final JLabel[] labels;
  76. private final JTextField[] textFields;
  77. private final String[] propertyNames;
  78. final ProjectCustomizer.Category category;
  79. public RunAsInternalServer(PhpProjectProperties properties, ConfigManager manager, ProjectCustomizer.Category category) {
  80. super(manager);
  81. this.properties = properties;
  82. this.category = category;
  83. project = properties.getProject();
  84. initComponents();
  85. this.labels = new JLabel[] {
  86. hostnameLabel,
  87. portLabel,
  88. routerLabel
  89. };
  90. this.textFields = new JTextField[] {
  91. hostnameTextField,
  92. portTextField,
  93. routerTextField
  94. };
  95. this.propertyNames = new String[] {
  96. PhpProjectProperties.HOSTNAME,
  97. PhpProjectProperties.PORT,
  98. PhpProjectProperties.ROUTER
  99. };
  100. assert labels.length == textFields.length && labels.length == propertyNames.length;
  101. for (int i = 0; i < textFields.length; i++) {
  102. DocumentListener dl = new FieldUpdater(propertyNames[i], labels[i], textFields[i]);
  103. textFields[i].getDocument().addDocumentListener(dl);
  104. }
  105. }
  106. @Override
  107. protected RunAsType getRunAsType() {
  108. return RunConfigInternal.getRunAsType();
  109. }
  110. @Override
  111. protected String getDisplayName() {
  112. return RunConfigInternal.getDisplayName();
  113. }
  114. @Override
  115. protected JLabel getRunAsLabel() {
  116. return runAsLabel;
  117. }
  118. @Override
  119. protected JComboBox getRunAsCombo() {
  120. return runAsComboBox;
  121. }
  122. @Override
  123. protected void loadFields() {
  124. // XXX remove index.file from properties, so run/debug project can then work
  125. putValue(PhpProjectProperties.INDEX_FILE, null);
  126. for (int i = 0; i < textFields.length; i++) {
  127. String value = getValue(propertyNames[i]);
  128. if (value == null) {
  129. if (PhpProjectProperties.HOSTNAME.equals(propertyNames[i])) {
  130. value = RunConfigInternal.DEFAULT_HOSTNAME;
  131. } else if (PhpProjectProperties.PORT.equals(propertyNames[i])) {
  132. value = String.valueOf(RunConfigInternal.DEFAULT_PORT);
  133. }
  134. }
  135. textFields[i].setText(value);
  136. }
  137. }
  138. @Override
  139. protected void validateFields() {
  140. category.setErrorMessage(RunConfigInternalValidator.validateCustomizer(createRunConfig()));
  141. // #148957 always allow to save customizer
  142. category.setValid(true);
  143. }
  144. private RunConfigInternal createRunConfig() {
  145. return RunConfigInternal.create()
  146. .setHostname(hostnameTextField.getText())
  147. .setPort(portTextField.getText())
  148. .setWorkDir(getSources())
  149. .setDocumentRoot(getWebRoot())
  150. .setRouterRelativePath(routerTextField.getText());
  151. }
  152. private File getSources() {
  153. return FileUtil.toFile(ProjectPropertiesSupport.getSourcesDirectory(project));
  154. }
  155. private File getWebRoot() {
  156. return ProjectPropertiesSupport.getSourceSubdirectory(project, properties.getWebRoot());
  157. }
  158. /**
  159. * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form
  160. * Editor.
  161. */
  162. @SuppressWarnings("unchecked")
  163. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  164. private void initComponents() {
  165. runAsLabel = new JLabel();
  166. runAsComboBox = new JComboBox();
  167. hostnameLabel = new JLabel();
  168. hostnameTextField = new JTextField();
  169. portLabel = new JLabel();
  170. portTextField = new JTextField();
  171. urlHintLabel = new JLabel();
  172. routerLabel = new JLabel();
  173. routerTextField = new JTextField();
  174. routerBrowseButton = new JButton();
  175. noteLabel = new JLabel();
  176. phpVersionInfoLabel = new JLabel();
  177. Mnemonics.setLocalizedText(runAsLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.runAsLabel.text")); // NOI18N
  178. Mnemonics.setLocalizedText(hostnameLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.hostnameLabel.text")); // NOI18N
  179. hostnameTextField.setColumns(20);
  180. Mnemonics.setLocalizedText(portLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.portLabel.text")); // NOI18N
  181. portTextField.setColumns(20);
  182. Mnemonics.setLocalizedText(urlHintLabel, " "); // NOI18N
  183. Mnemonics.setLocalizedText(routerLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.routerLabel.text")); // NOI18N
  184. routerTextField.setColumns(20);
  185. Mnemonics.setLocalizedText(routerBrowseButton, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.routerBrowseButton.text")); // NOI18N
  186. routerBrowseButton.addActionListener(new ActionListener() {
  187. public void actionPerformed(ActionEvent evt) {
  188. routerBrowseButtonActionPerformed(evt);
  189. }
  190. });
  191. Mnemonics.setLocalizedText(noteLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.noteLabel.text")); // NOI18N
  192. Mnemonics.setLocalizedText(phpVersionInfoLabel, NbBundle.getMessage(RunAsInternalServer.class, "RunAsInternalServer.phpVersionInfoLabel.text")); // NOI18N
  193. GroupLayout layout = new GroupLayout(this);
  194. this.setLayout(layout);
  195. layout.setHorizontalGroup(
  196. layout.createParallelGroup(Alignment.LEADING)
  197. .addGroup(layout.createSequentialGroup()
  198. .addGroup(layout.createParallelGroup(Alignment.LEADING)
  199. .addComponent(routerLabel)
  200. .addComponent(portLabel)
  201. .addComponent(runAsLabel)
  202. .addComponent(hostnameLabel))
  203. .addPreferredGap(ComponentPlacement.RELATED)
  204. .addGroup(layout.createParallelGroup(Alignment.LEADING)
  205. .addGroup(layout.createSequentialGroup()
  206. .addComponent(routerTextField, GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
  207. .addPreferredGap(ComponentPlacement.RELATED)
  208. .addComponent(routerBrowseButton))
  209. .addGroup(layout.createSequentialGroup()
  210. .addComponent(urlHintLabel)
  211. .addContainerGap())
  212. .addComponent(runAsComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  213. .addComponent(hostnameTextField, GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
  214. .addGroup(layout.createSequentialGroup()
  215. .addComponent(portTextField, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
  216. .addGap(0, 0, Short.MAX_VALUE))))
  217. .addGroup(layout.createSequentialGroup()
  218. .addContainerGap()
  219. .addComponent(phpVersionInfoLabel)
  220. .addContainerGap())
  221. .addGroup(layout.createSequentialGroup()
  222. .addComponent(noteLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  223. .addGap(0, 0, Short.MAX_VALUE))
  224. );
  225. layout.setVerticalGroup(
  226. layout.createParallelGroup(Alignment.LEADING)
  227. .addGroup(layout.createSequentialGroup()
  228. .addGroup(layout.createParallelGroup(Alignment.BASELINE)
  229. .addComponent(runAsLabel)
  230. .addComponent(runAsComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  231. .addGap(18, 18, 18)
  232. .addGroup(layout.createParallelGroup(Alignment.BASELINE)
  233. .addComponent(hostnameLabel)
  234. .addComponent(hostnameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  235. .addPreferredGap(ComponentPlacement.RELATED)
  236. .addGroup(layout.createParallelGroup(Alignment.BASELINE)
  237. .addComponent(portLabel)
  238. .addComponent(portTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  239. .addPreferredGap(ComponentPlacement.RELATED)
  240. .addComponent(urlHintLabel)
  241. .addPreferredGap(ComponentPlacement.RELATED)
  242. .addGroup(layout.createParallelGroup(Alignment.BASELINE)
  243. .addComponent(routerLabel)
  244. .addComponent(routerTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  245. .addComponent(routerBrowseButton))
  246. .addGap(18, 18, 18)
  247. .addComponent(noteLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  248. .addPreferredGap(ComponentPlacement.RELATED)
  249. .addComponent(phpVersionInfoLabel)
  250. .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  251. );
  252. }// </editor-fold>//GEN-END:initComponents
  253. private void routerBrowseButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_routerBrowseButtonActionPerformed
  254. try {
  255. Utils.browseFolderFile(PhpVisibilityQuery.forProject(project), getSources(), routerTextField);
  256. } catch (FileNotFoundException ex) {
  257. // cannot happen for sources
  258. Exceptions.printStackTrace(ex);
  259. }
  260. }//GEN-LAST:event_routerBrowseButtonActionPerformed
  261. // Variables declaration - do not modify//GEN-BEGIN:variables
  262. private JLabel hostnameLabel;
  263. private JTextField hostnameTextField;
  264. private JLabel noteLabel;
  265. private JLabel phpVersionInfoLabel;
  266. private JLabel portLabel;
  267. private JTextField portTextField;
  268. private JButton routerBrowseButton;
  269. private JLabel routerLabel;
  270. private JTextField routerTextField;
  271. private JComboBox runAsComboBox;
  272. private JLabel runAsLabel;
  273. private JLabel urlHintLabel;
  274. // End of variables declaration//GEN-END:variables
  275. //~ Inner classes
  276. private class FieldUpdater extends TextFieldUpdater {
  277. public FieldUpdater(String propName, JLabel label, JTextField field) {
  278. super(propName, label, field);
  279. }
  280. @Override
  281. protected final String getDefaultValue() {
  282. return RunAsInternalServer.this.getDefaultValue(getPropName());
  283. }
  284. @Override
  285. protected void processUpdate() {
  286. super.processUpdate();
  287. updateAndSetUrl();
  288. }
  289. // update hint & store project url as well (for running/debugging the project)
  290. private void updateAndSetUrl() {
  291. String url = createRunConfig().getUrlHint();
  292. urlHintLabel.setText(url != null ? "<html><body>" + url : " "); // NOI18N
  293. putValue(PhpProjectProperties.URL, url);
  294. }
  295. }
  296. }