001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makewizard;
043:
044: import java.awt.GridBagConstraints;
045: import java.awt.GridBagLayout;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.awt.event.FocusEvent;
049: import java.awt.event.FocusListener;
050: import java.awt.event.KeyAdapter;
051: import java.awt.event.KeyEvent;
052: import javax.swing.JComboBox;
053: import javax.swing.JLabel;
054: import javax.swing.JPanel;
055: import javax.swing.JTextArea;
056: import javax.swing.JTextField;
057: import javax.swing.text.JTextComponent;
058: import org.netbeans.modules.cnd.api.utils.IpeUtils;
059:
060: /**
061: * Create the Sources panel in the Makefile wizard.
062: */
063: /* Implements FocusListener because if you selected something in a text field,
064: * then clicked in a different text field, your selection would persist in the
065: * previous text field. Need to lose the selection when you lose the focus.
066: * This issue is when using jdk 1.4.0.
067: */
068: public final class CompilerOptionsPanel extends MakefileWizardPanel
069: implements FocusListener {
070:
071: /** Serial version number */
072: static final long serialVersionUID = 5260017369797781413L;
073:
074: private JTextArea topLabel;
075: private JTextField cText;
076: private JTextField cppText;
077: private JTextField fortranText;
078: private JTextField basicText;
079: private CompilerFlags copts;
080: private JLabel cppCompilerLabel;
081: private JComboBox cConformLevelCB;
082: private ActionListener cConformLevelCBActionListener;
083: private JComboBox cppConformLevelCB;
084: private ActionListener cppConformLevelCBActionListener;
085:
086: private boolean initialized;
087:
088: /** Constructor for the compiler options panel */
089: public CompilerOptionsPanel(MakefileWizard wd) {
090: super (wd);
091: String subtitle = getString("LBL_CompilerOptionsPanel"); // NOI18N
092: setSubTitle(subtitle);
093: this .getAccessibleContext().setAccessibleDescription(subtitle);
094: initialized = false;
095: }
096:
097: /**
098: * Create the panel. Since this class is used as a superclass for several
099: * panels information is passed in to allow some customization at creation
100: * time.
101: *
102: * @param label The label for the text field at the top of the panel
103: * @param flags Set some optional flags to override default behavior
104: */
105: protected void create() {
106: int gridy = 0;
107:
108: setLayout(new GridBagLayout());
109: GridBagConstraints grid = new GridBagConstraints();
110: grid.anchor = GridBagConstraints.NORTHWEST;
111: grid.gridx = 0;
112:
113: topLabel = new JTextArea(getString("LBL_COptsTopLabel")); // NOI18N
114: topLabel.addFocusListener(this );
115: topLabel.setEditable(false);
116: topLabel.setFocusable(false);
117: topLabel.setBackground(new JPanel().getBackground());
118: grid.gridy = gridy++;
119: grid.fill = GridBagConstraints.HORIZONTAL;
120: grid.gridwidth = GridBagConstraints.REMAINDER;
121: grid.weightx = 1.0;
122: add(topLabel, grid);
123:
124: grid.insets.top = 5;
125:
126: // C language fields
127: JLabel cLabel = new JLabel(getString("LBL_COptsCCompiler")); // NOI18N
128: cLabel.setDisplayedMnemonic(getString("MNEM_COptsCCompiler")
129: .charAt(0)); // NOI18N
130: grid.gridx = 0;
131: grid.gridy = gridy++;
132: grid.fill = GridBagConstraints.NONE;
133: grid.gridwidth = 1;
134: grid.insets.right = 11;
135: grid.insets.top = 11;
136: grid.weightx = 0.0;
137: add(cLabel, grid);
138:
139: cText = new JTextField();
140: cText.addFocusListener(this );
141: cLabel.setLabelFor(cText);
142: grid.gridx = 1;
143: grid.fill = GridBagConstraints.HORIZONTAL;
144: grid.gridwidth = GridBagConstraints.REMAINDER;
145: grid.insets.right = 0;
146: add(cText, grid);
147:
148: JLabel cCompilerLabel = new javax.swing.JLabel();
149: cCompilerLabel.setText(getString("LBL_ConformLevel")); // NOI18N
150: cCompilerLabel.setDisplayedMnemonic(getString(
151: "MNEM_ConformLevel1").charAt(0)); // NOI18N
152: grid.gridx = 1;
153: grid.gridy = ++gridy;
154: grid.gridwidth = 1;
155: grid.fill = GridBagConstraints.NONE;
156: grid.insets = new java.awt.Insets(8, 0, 0, 0);
157: grid.anchor = java.awt.GridBagConstraints.NORTHWEST;
158: add(cCompilerLabel, grid);
159:
160: cConformLevelCB = new JComboBox();
161: cCompilerLabel.setLabelFor(cConformLevelCB);
162: grid.gridx = 2;
163: grid.gridy = gridy++;
164: grid.gridwidth = 1;
165: grid.fill = GridBagConstraints.NONE;
166: grid.insets = new java.awt.Insets(5, 5, 0, 0);
167: grid.anchor = java.awt.GridBagConstraints.NORTHWEST;
168: add(cConformLevelCB, grid);
169:
170: // C++ language fields
171: JLabel cppLabel = new JLabel(getString("LBL_COptsCppCompiler")); // NOI18N
172: cppLabel
173: .setDisplayedMnemonic(getString("MNEM_COptsCppCompiler")
174: .charAt(0)); // NOI18N
175: grid.gridx = 0;
176: grid.gridy = gridy++;
177: grid.fill = GridBagConstraints.NONE;
178: grid.gridwidth = 1;
179: grid.insets = new java.awt.Insets(0, 0, 0, 0);
180: grid.insets.right = 11;
181: grid.insets.top = 11;
182: grid.weightx = 0.0;
183: add(cppLabel, grid);
184:
185: cppText = new JTextField();
186: cppText.addFocusListener(this );
187: cppLabel.setLabelFor(cppText);
188: grid.gridx = 1;
189: grid.fill = GridBagConstraints.HORIZONTAL;
190: grid.gridwidth = GridBagConstraints.REMAINDER;
191: grid.insets.right = 0;
192: add(cppText, grid);
193:
194: cppCompilerLabel = new javax.swing.JLabel();
195: grid.gridx = 1;
196: grid.gridy = ++gridy;
197: grid.gridwidth = 1;
198: grid.fill = GridBagConstraints.NONE;
199: grid.insets = new java.awt.Insets(8, 0, 0, 0);
200: grid.anchor = java.awt.GridBagConstraints.NORTHWEST;
201: add(cppCompilerLabel, grid);
202:
203: cppConformLevelCB = new JComboBox();
204: cppCompilerLabel.setLabelFor(cppConformLevelCB);
205: grid.gridx = 2;
206: grid.gridy = gridy++;
207: grid.gridwidth = 1;
208: grid.fill = GridBagConstraints.NONE;
209: grid.insets = new java.awt.Insets(5, 5, 0, 0);
210: grid.anchor = java.awt.GridBagConstraints.NORTHWEST;
211: add(cppConformLevelCB, grid);
212:
213: // Fortran language fields
214: JLabel fortranLabel = new JLabel(
215: getString("LBL_COptsFortranCompiler")); // NOI18N
216: fortranLabel.setDisplayedMnemonic(getString(
217: "MNEM_COptsFortranCompiler").charAt(0)); // NOI18N
218: grid.gridx = 0;
219: grid.gridy = gridy++;
220: grid.fill = GridBagConstraints.NONE;
221: grid.gridwidth = 1;
222: grid.insets = new java.awt.Insets(0, 0, 0, 0);
223: grid.insets.right = 11;
224: grid.insets.top = 11;
225: grid.weightx = 0.0;
226: add(fortranLabel, grid);
227:
228: fortranText = new JTextField();
229: fortranText.addFocusListener(this );
230: fortranLabel.setLabelFor(fortranText);
231: grid.gridx = 1;
232: grid.fill = GridBagConstraints.HORIZONTAL;
233: grid.gridwidth = GridBagConstraints.REMAINDER;
234: grid.insets.right = 0;
235: add(fortranText, grid);
236:
237: // Show a read-only image of Basic Options
238: JLabel basicLabel = new JLabel(getString("LBL_BasicOptionsVar")); // NOI18N
239: grid.gridx = 0;
240: grid.gridy = gridy++;
241: grid.fill = GridBagConstraints.NONE;
242: grid.gridwidth = 1;
243: grid.insets.right = 11;
244: grid.weightx = 0.0;
245: add(basicLabel, grid);
246:
247: basicText = new JTextField();
248: basicText.addFocusListener(this );
249: basicText.setEditable(false);
250: basicText.setFocusable(false);
251: basicText.setBackground(basicLabel.getBackground());
252: basicLabel.setLabelFor(basicText);
253: grid.gridwidth = GridBagConstraints.REMAINDER;
254: grid.gridx = 1;
255: grid.fill = GridBagConstraints.HORIZONTAL;
256: grid.gridwidth = GridBagConstraints.REMAINDER;
257: grid.insets.right = 0;
258: grid.weightx = 1.0;
259: grid.weighty = 1.0;
260: add(basicText, grid);
261:
262: // Fill in extra space below the last visible widget
263: grid.gridy = gridy++;
264: grid.gridheight = GridBagConstraints.REMAINDER;
265: add(new JLabel(""), grid); // NOI18N
266:
267: topLabel.setBackground(cLabel.getBackground());
268:
269: // Add action and key listeners
270: cConformLevelCB
271: .addActionListener(cConformLevelCBActionListener = new ActionListener() {
272: public void actionPerformed(ActionEvent evt) {
273: cConformLevelCBActionPerformed(evt);
274: }
275: });
276:
277: cText.addKeyListener(new KeyAdapter() {
278: public void keyReleased(KeyEvent evt) {
279: cTextKeyReleased(evt);
280: }
281: });
282:
283: cppConformLevelCB
284: .addActionListener(cppConformLevelCBActionListener = new ActionListener() {
285: public void actionPerformed(ActionEvent evt) {
286: cppConformLevelCBActionPerformed(evt);
287: }
288: });
289:
290: cppText.addKeyListener(new KeyAdapter() {
291: public void keyReleased(KeyEvent evt) {
292: cppTextKeyReleased(evt);
293: }
294: });
295: }
296:
297: /** Initialize the display values as we enter the panel */
298: public void addNotify() {
299:
300: if (!initialized) {
301: initialized = true;
302:
303: create();
304: copts = getMakefileData().getCompilerFlags();
305: }
306:
307: super .addNotify();
308:
309: cText.setText(copts.getCFlags(getMakefileData().getToolset()));
310: cppText.setText(copts
311: .getCcFlags(getMakefileData().getToolset()));
312: fortranText.setText(copts.getF90Flags());
313: basicText.setText(copts.getBasicOptions(getMakefileData()
314: .getToolset()));
315: cText.selectAll();
316: IpeUtils.requestFocus(cText);
317:
318: // Compiler conformance level
319: cConformLevelCB
320: .removeActionListener(cConformLevelCBActionListener);
321: cppConformLevelCB
322: .removeActionListener(cppConformLevelCBActionListener);
323: cConformLevelCB.removeAllItems();
324: cppConformLevelCB.removeAllItems();
325: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
326: cConformLevelCB.addItem(getString("CSunConform0"));
327: cConformLevelCB.addItem(getString("CSunConform1"));
328: cConformLevelCB.addItem(getString("CSunConform2"));
329: cConformLevelCB.setSelectedIndex(getMakefileData()
330: .getConformLevelCSun());
331:
332: cppCompilerLabel.setText(getString("LBL_Compatibility"));
333: cppCompilerLabel.setDisplayedMnemonic(getString(
334: "MNEM_Compatibility").charAt(0)); // NOI18N
335: cppConformLevelCB.addItem(getString("CppSunConform0"));
336: cppConformLevelCB.addItem(getString("CppSunConform1"));
337: cppConformLevelCB.setSelectedIndex(getMakefileData()
338: .getConformLevelCppSun());
339: } else {
340: cConformLevelCB.addItem(getString("CGNUConform0"));
341: cConformLevelCB.addItem(getString("CGNUConform1"));
342: cConformLevelCB.setSelectedIndex(getMakefileData()
343: .getConformLevelCGNU());
344:
345: cppCompilerLabel.setText(getString("LBL_ConformLevel"));
346: cppCompilerLabel.setDisplayedMnemonic(getString(
347: "MNEM_ConformLevel2").charAt(0)); // NOI18N
348: cppConformLevelCB.addItem(getString("CppGNUConform0"));
349: cppConformLevelCB.addItem(getString("CppGNUConform1"));
350: cppConformLevelCB.setSelectedIndex(getMakefileData()
351: .getConformLevelCppGNU());
352: }
353: cConformLevelCB
354: .addActionListener(cConformLevelCBActionListener);
355: cppConformLevelCB
356: .addActionListener(cppConformLevelCBActionListener);
357: }
358:
359: /** Check the display values as we leave the panel and update CompilerFlags if needed */
360: public void removeNotify() {
361: super .removeNotify();
362:
363: copts
364: .setCFlags(getMakefileData().getToolset(), cText
365: .getText());
366: copts.setCcFlags(getMakefileData().getToolset(), cppText
367: .getText());
368: copts.setF90Flags(fortranText.getText());
369:
370: // Compiler conformance level
371: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
372: getMakefileData().setConformLevelCSun(
373: cConformLevelCB.getSelectedIndex());
374: getMakefileData().setConformLevelCppSun(
375: cppConformLevelCB.getSelectedIndex());
376: } else {
377: getMakefileData().setConformLevelCGNU(
378: cConformLevelCB.getSelectedIndex());
379: getMakefileData().setConformLevelCppGNU(
380: cppConformLevelCB.getSelectedIndex());
381: }
382: }
383:
384: public void focusGained(FocusEvent evt) {
385: if (evt.getComponent() == basicText
386: || evt.getComponent() == topLabel) {
387: ((JTextComponent) evt.getComponent()).selectAll();
388: }
389: }
390:
391: public void focusLost(FocusEvent evt) {
392: ((JTextComponent) evt.getComponent()).setSelectionEnd(0);
393: }
394:
395: /**
396: * Track c conformance level combobox selection and transfer option to textfield
397: */
398: private void cConformLevelCBActionPerformed(ActionEvent evt) {
399: String newTxt;
400: String[] levels;
401: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
402: levels = MakefileData.conformLevelsCSun;
403: } else {
404: levels = MakefileData.conformLevelsCGNU;
405: }
406: int i = cConformLevelCB.getSelectedIndex();
407:
408: newTxt = removeOptions(cText.getText(), levels);
409: newTxt = addOption(newTxt, levels[i]);
410: cText.setText(newTxt);
411: }
412:
413: /**
414: * Track cpp conformance level combobox selection and transfer option to textfield
415: */
416: private void cppConformLevelCBActionPerformed(ActionEvent evt) {
417: String newTxt;
418: String[] levels;
419: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
420: levels = MakefileData.conformLevelsCppSun;
421: } else {
422: levels = MakefileData.conformLevelsCppGNU;
423: }
424: int i = cppConformLevelCB.getSelectedIndex();
425:
426: newTxt = removeOptions(cppText.getText(), levels);
427: newTxt = addOption(newTxt, levels[i]);
428: cppText.setText(newTxt);
429: }
430:
431: /**
432: * Track c conformance level textfield and transfer options to combobox selection
433: */
434: private void cTextKeyReleased(java.awt.event.KeyEvent evt) {
435: String txt = cText.getText();
436: String[] levels;
437: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
438: levels = MakefileData.conformLevelsCSun;
439: } else {
440: levels = MakefileData.conformLevelsCGNU;
441: }
442: int def = 0;
443: boolean foundOne = false;
444:
445: cConformLevelCB
446: .removeActionListener(cConformLevelCBActionListener);
447: for (int i = 0; i < levels.length; i++) {
448: if (levels[i].length() == 0) {
449: def = i;// The defalut value if no options found
450: continue;
451: }
452: if (containsOption(txt, levels[i]) >= 0) {
453: cConformLevelCB.setSelectedIndex(i);
454: foundOne = true;
455: break;
456: }
457: }
458: if (!foundOne) {
459: cConformLevelCB.setSelectedIndex(def);
460: }
461: cConformLevelCB
462: .addActionListener(cConformLevelCBActionListener);
463: }
464:
465: /**
466: * Track cpp conformance level textfield and transfer options to combobox selection
467: */
468: private void cppTextKeyReleased(java.awt.event.KeyEvent evt) {
469: String txt = cppText.getText();
470: String[] levels;
471: if (getMakefileData().getToolset() == MakefileData.SUN_TOOLSET_TYPE) {
472: levels = MakefileData.conformLevelsCppSun;
473: } else {
474: levels = MakefileData.conformLevelsCppGNU;
475: }
476: int def = 0;
477: boolean foundOne = false;
478:
479: cppConformLevelCB
480: .removeActionListener(cppConformLevelCBActionListener);
481: for (int i = 0; i < levels.length; i++) {
482: if (levels[i].length() == 0) {
483: def = i;// The defalut value if no options found
484: continue;
485: }
486: if (containsOption(txt, levels[i]) >= 0) {
487: cppConformLevelCB.setSelectedIndex(i);
488: foundOne = true;
489: break;
490: }
491: }
492: if (!foundOne) {
493: cppConformLevelCB.setSelectedIndex(def);
494: }
495: cppConformLevelCB
496: .addActionListener(cppConformLevelCBActionListener);
497: }
498:
499: /**
500: * returns position of option 'option' in a text String. Returns -1 if not found.
501: */
502: private int containsOption(String txt, String option) {
503: int i;
504: int start = 0;
505: int lTxt = txt.length();
506: int lOpt = option.length();
507:
508: while (start <= (lTxt - lOpt)) {
509: i = txt.indexOf(option, start);
510: if (i < 0) {
511: return -1;
512: }
513: if (i > 0 && !Character.isWhitespace(txt.charAt(i - 1))) {
514: start = i + 1;
515: continue;
516: }
517: if (i < (lTxt - lOpt)
518: && !Character.isWhitespace(txt.charAt(i + lOpt))) {
519: start = i + 1;
520: continue;
521: }
522: return i;
523: }
524: return -1;
525: }
526:
527: /**
528: * Removes an array of options 'options' from text String. Returns modified text string.
529: */
530: private String removeOptions(String txt, String[] options) {
531: String newTxt = txt;
532: for (int i = 0; i < options.length; i++) {
533: newTxt = removeOption(newTxt, options[i]);
534: }
535: return newTxt;
536: }
537:
538: /**
539: * Removes option 'option' from text String. Returns modified text string.
540: */
541: private String removeOption(String txt, String option) {
542: int i;
543: String newTxt;
544:
545: if (option == null || option.length() == 0)
546: return txt;
547:
548: i = containsOption(txt, option);
549: if (i < 0)
550: return txt;
551:
552: if (i == 0) {
553: newTxt = txt.substring(i + option.length());
554: } else {
555: newTxt = txt.substring(0, i - 1)
556: + txt.substring(i + option.length());
557: }
558: return newTxt;
559: }
560:
561: /**
562: * Add an option 'option' to text String (and possible also a space). Returns modified text string.
563: */
564: private String addOption(String txt, String option) {
565: int i;
566: String white = ""; // NOI18N
567: String newTxt;
568:
569: i = containsOption(txt, option);
570: if (i >= 0)
571: return txt;
572:
573: if (txt.length() > 0
574: && !Character
575: .isWhitespace(txt.charAt(txt.length() - 1)))
576: white = " "; // NOI18N
577: newTxt = txt + white + option;
578:
579: return newTxt;
580: }
581: }
|