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 java.io.File;
053: import java.io.IOException;
054: import java.util.ArrayList;
055: import javax.swing.JButton;
056: import javax.swing.JLabel;
057: import javax.swing.JScrollPane;
058: import javax.swing.JTextArea;
059: import org.netbeans.modules.cnd.api.utils.IpeUtils;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * Create the third panel in the Makefile wizard.
064: */
065:
066: public class MakefileReviewPanel extends MakefileWizardPanel implements
067: FocusListener {
068:
069: /** Serial version number */
070: static final long serialVersionUID = 6675031915575184904L;
071:
072: // the fields in the first panel...
073: private JLabel reviewLabel;
074: private JTextArea reviewText;
075: private JScrollPane reviewSP;
076:
077: /** Store the Finish button's label */
078: private String finishLabel;
079:
080: /** Store the Finish button's mnemonic */
081: private int finishMnemonic;
082:
083: /** Build the summary text in this StringBuffer */
084: private StringBuffer summary;
085:
086: private boolean initialized;
087:
088: /* Flag to disable text selection when panel is first displayed */
089: private boolean enableTextSelection = false;
090:
091: ActionListener finishButtonListener = null;
092:
093: /**
094: * Constructor for the Makefile name panel. Remember, most of the panel is
095: * inherited from WizardDescriptor.
096: */
097: MakefileReviewPanel(MakefileWizard wd) {
098: super (wd);
099: String subtitle = new String(
100: getString("LBL_MakefileReviewPanel")); // NOI18N
101: setSubTitle(subtitle);
102: this .getAccessibleContext().setAccessibleDescription(
103: subtitle + getString("ACSD_MakefileReview")); // NOI18N
104: initialized = false;
105: }
106:
107: /** Defer widget creation until the panel needs to be displayed */
108: private void create() {
109:
110: setLayout(new GridBagLayout());
111:
112: // Create the review label
113: reviewLabel = new JLabel();
114: reviewLabel.setText(NbBundle.getMessage(
115: MakefileReviewPanel.class, "LBL_Summary")); // NOI18N
116: reviewLabel.setDisplayedMnemonic((NbBundle.getMessage(
117: MakefileReviewPanel.class, "MNEM_Summary")).charAt(0)); // NOI18N
118:
119: // Set the GridBagLayout constraints for the label
120: GridBagConstraints grid = new GridBagConstraints();
121: grid.anchor = GridBagConstraints.NORTHWEST;
122: grid.gridx = 0;
123: grid.gridy = 1;
124: grid.gridheight = 1;
125: grid.insets = new java.awt.Insets(0, 0, 5, 0);
126: add(reviewLabel, grid);
127:
128: // Create the component.
129: reviewText = new JTextArea();
130: reviewText.setEditable(false);
131: reviewText.getCaret().setVisible(false);
132: reviewText.getCaret().setSelectionVisible(false);
133: reviewText.setBackground(getBackground());
134: reviewText.addFocusListener(this );
135: reviewLabel.setLabelFor(reviewText);
136:
137: reviewSP = new JScrollPane(reviewText);
138: reviewSP.getViewport()
139: .setBackground(reviewText.getBackground());
140:
141: // Set the GridBagLayout constraints.
142: grid = new GridBagConstraints();
143: grid.gridx = 0;
144: grid.gridy = 2;
145: grid.weightx = 100.0;
146: grid.weighty = 100.0;
147: grid.fill = GridBagConstraints.BOTH;
148: add(reviewSP, grid);
149:
150: if (IpeUtils.IfdefDiagnostics) {
151: reviewText.addKeyListener(new KeyAdapter() {
152:
153: public void keyPressed(KeyEvent ev) {
154:
155: int mods = ev.getModifiers();
156: if (ev.isControlDown()
157: && ev.getKeyCode() == KeyEvent.VK_P) {
158: getMakefileData().dump();
159: }
160: }
161: });
162: }
163: }
164:
165: /** Summarize the information gathered in the MakefileData */
166: private String getSummaryString() {
167: MakefileData md = getMakefileData();
168: String base = md.getBaseDirectory(MakefileData.EXPAND);
169: String makefile = md.getMakefileName();
170: String dir = getMakefileDirectory(base, makefile);
171: String name = getMakefileName(makefile);
172:
173: summary = new StringBuffer(1024);
174: append("LBL_MakefileSummaryDir", dir); // NOI18N
175: append("LBL_MakefileSummaryName", name); // NOI18N
176: append("LBL_BuildInstructions"); // NOI18N
177: append("LBL_BuildInstruction1", name, dir); // NOI18N
178: append("LBL_BuildInstruction2", name); // NOI18N
179:
180: append("LBL_MakefileSpecifics"); // NOI18N
181: if (md.getTargetList().size() == 1
182: || md.getMakefileType() < MakefileData.COMPLEX_MAKEFILE_TYPE) {
183: TargetData td = (TargetData) md.getTargetList().get(0);
184:
185: if (td.isExecutable()) {
186: append("LBL_SingleExe", td.getName()); // NOI18N
187: } else if (td.isArchive()) {
188: append("LBL_SingleArchive", td.getName()); // NOI18N
189: } else if (td.isSharedLib()) {
190: append("LBL_SingleSharedLib", td.getName()); // NOI18N
191: } else if (td.isMakeTarget()) {
192: append("LBL_SingleMake", td.getName()); // NOI18N
193: } else if (td.isCustomTarget()) {
194: append("LBL_SingleCustom", td.getName()); // NOI18N
195: } else if (IpeUtils.IfdefDiagnostics) {
196: System.out
197: .println("Error: Unknown target type in summary"); // NOI18N
198: }
199: if (td.getSourcesList() == null) {
200: append("LBL_SingleTargetSrcs", new Integer(0)); // NOI18N
201: } else {
202: if (td.getSourcesList().length == 1) {
203: append("LBL_SingleTargetSrcs1"); // NOI18N
204: } else {
205: append("LBL_SingleTargetSrcs", // NOI18N
206: new Integer(td.getSourcesList().length));
207: }
208: }
209: } else {
210: ArrayList tlist = md.getTargetList();
211:
212: append("LBL_MultiTarget"); // NOI18N
213: for (int i = 0; i < tlist.size(); i++) {
214: TargetData td = (TargetData) tlist.get(i);
215:
216: int srcCnt;
217: if (td.getSourcesList() == null) {
218: srcCnt = 0;
219: } else {
220: srcCnt = td.getSourcesList().length;
221: }
222:
223: if (td.isExecutable()) {
224: if (srcCnt == 1) {
225: append("LBL_MultiExe1", td.getName()); // NOI18N
226: } else {
227: append("LBL_MultiExe", td.getName(), // NOI18N
228: new Integer(srcCnt));
229: }
230: } else if (td.isArchive()) {
231: if (srcCnt == 1) {
232: append("LBL_MultiArchive1", td.getName()); // NOI18N
233: } else {
234: append("LBL_MultiArchive", td.getName(), // NOI18N
235: new Integer(srcCnt));
236: }
237: } else if (td.isSharedLib()) {
238: if (srcCnt == 1) {
239: append("LBL_MultiSharedLib1", td.getName()); // NOI18N
240: } else {
241: append("LBL_MultiSharedLib", td.getName(), // NOI18N
242: new Integer(srcCnt));
243: }
244: } else if (td.isMakeTarget()) {
245: append("LBL_MultiMake", td.getName()); // NOI18N
246: } else if (td.isCustomTarget()) {
247: if (srcCnt == 1) {
248: append("LBL_MultiCustom1", td.getName()); // NOI18N
249: } else {
250: append("LBL_MultiCustom", td.getName(), // NOI18N
251: new Integer(srcCnt));
252: }
253: } else if (IpeUtils.IfdefDiagnostics) {
254: System.out
255: .println("Error: Unknown target type in summary"); // NOI18N
256: }
257: }
258: }
259:
260: if (isDebug() && !isOptimize()) {
261: append("LBL_DebugCompile"); // NOI18N
262: } else if (isDebug() && isOptimize()) {
263: append("LBL_DebugOptCompile"); // NOI18N
264: } else if (isOptimize()) {
265: append("LBL_OptimizeCompile"); // NOI18N
266: } else if (IpeUtils.IfdefDiagnostics) {
267: append("LBL_NoOptDebugCompile"); // NOI18N
268: }
269:
270: validateAllData();
271: append("LBL_Finish"); // NOI18N
272:
273: return summary.toString();
274: }
275:
276: private String getMakefileDirectory(String base, String makefile) {
277:
278: // First, make sure makefile is an absolute path
279: if (makefile.charAt(0) != File.separatorChar) {
280: makefile = base + File.separator + makefile;
281: }
282:
283: // Now get its canonical form
284: File file = new File(makefile);
285: String path;
286:
287: try {
288: path = file.getCanonicalPath();
289: } catch (IOException ex) {
290: return makefile.substring(0, makefile
291: .lastIndexOf(File.separator) - 1);
292: }
293:
294: return path.substring(0, path.lastIndexOf(File.separator));
295: }
296:
297: private String getMakefileName(String makefile) {
298:
299: int pos = makefile.lastIndexOf(File.separator);
300: if (pos >= 0) {
301: return makefile.substring(pos + 1);
302: } else {
303: return makefile;
304: }
305: }
306:
307: /** Shortcut for checking debug flags */
308: private boolean isDebug() {
309: MakefileData md = getMakefileData();
310: CompilerFlags copts = md.getCompilerFlags();
311:
312: if (md.getMakefileType() < MakefileData.COMPLEX_MAKEFILE_TYPE) {
313: return copts.isSimpleDebug();
314: } else if (copts.getOptionSource() == OptionSource.DEVELOPMENT) {
315: return true;
316: }
317:
318: return false;
319: }
320:
321: /** Shortcut for checking optization */
322: private boolean isOptimize() {
323: MakefileData md = getMakefileData();
324: CompilerFlags copts = md.getCompilerFlags();
325:
326: if (md.getMakefileType() < MakefileData.COMPLEX_MAKEFILE_TYPE) {
327: return copts.isSimpleOptimize();
328: } else if (copts.getOptionSource() == OptionSource.FINAL) {
329: return copts.isFinalOptimize();
330: } else if (copts.getOptionSource() == OptionSource.DEVELOPMENT) {
331: return !copts.isDevelDebug();
332: } else {
333: return false;
334: }
335: }
336:
337: /**
338: * Validate the MakefileData and all targets. Add any warnings to the
339: * summary in the Warnings area.
340: */
341: private void validateAllData() {
342: ArrayList msgs = MakefileWizard.getMakefileWizard()
343: .validateAllData();
344:
345: append("LBL_Warnings"); // NOI18N
346: if (msgs.size() == 0) {
347: append("LBL_NoWarnings"); // NOI18N
348: } else {
349: for (int i = 0; i < msgs.size(); i++) {
350: append(msgs.get(i));
351: }
352: }
353: }
354:
355: /** Generate the summary informatin and display it */
356: public void addNotify() {
357: if (!initialized) {
358: create();
359: initialized = true;
360: }
361:
362: super .addNotify();
363: reviewText.setText(getSummaryString());
364: reviewText.setCaretPosition(0);
365:
366: MakefileWizard mw = MakefileWizard.getMakefileWizard();
367: mw.getNextButton().setEnabled(false);
368: mw.setFinishEnabled(true);
369: JButton finishButton = mw.getFinishButton();
370:
371: finishLabel = finishButton.getText();
372: finishMnemonic = finishButton.getMnemonic();
373: finishButton.setText(mw.getFinishLabel());
374: finishButton.setMnemonic(mw.getFinishMnemonic());
375: finishButton.setEnabled(true);
376: getRootPane().setDefaultButton(finishButton);
377: IpeUtils.requestFocus(finishButton);
378: enableTextSelection = false;
379:
380: if (finishButtonListener == null) {
381: finishButtonListener = new ActionListener() {
382: public void actionPerformed(ActionEvent evt) {
383: finishButtonListenerActionPerformed();
384: }
385: };
386: }
387: finishButton.addActionListener(finishButtonListener);
388: }
389:
390: public void removeNotify() {
391: super .removeNotify();
392:
393: JButton finishButton = MakefileWizard.getMakefileWizard()
394: .getFinishButton();
395: finishButton.setText(finishLabel);
396: finishButton.setMnemonic(finishMnemonic);
397: getRootPane().setDefaultButton(
398: MakefileWizard.getMakefileWizard().getNextButton());
399:
400: finishButton.removeActionListener(finishButtonListener);
401: }
402:
403: public void finishButtonListenerActionPerformed() {
404: MakefileWizard mw = MakefileWizard.getMakefileWizard();
405: mw.setFinishClosingEnabled(true);
406: }
407:
408: // The following methods are related to displaying summary information
409: /** Append the a single message to the StringBuffer */
410: private void append(Object msg) {
411: summary.append(msg.toString());
412: }
413:
414: /** Append the a single message to the StringBuffer */
415: private void append(String msg) {
416: summary.append(NbBundle.getBundle(MakefileReviewPanel.class)
417: .getString(msg));
418: }
419:
420: /** Append the a message with a single arg to the StringBuffer */
421: private void append(String msg, Object arg1) {
422:
423: if (arg1 instanceof Integer && ((Integer) arg1).intValue() == 1) {
424: msg = msg + "1"; // NOI18N
425: }
426:
427: summary.append(NbBundle.getMessage(MakefileReviewPanel.class,
428: msg, arg1));
429: }
430:
431: /** Append the a message with a single arg to the StringBuffer */
432: private void append(String msg, Object arg1, Object arg2) {
433:
434: if (arg2 instanceof Integer && ((Integer) arg2).intValue() == 1) {
435: msg = msg + "1"; // NOI18N
436: }
437:
438: summary.append(NbBundle.getMessage(MakefileReviewPanel.class,
439: msg, arg1, arg2));
440: }
441:
442: public void focusGained(FocusEvent evt) {
443: // don't select text when panel is first displayed otherwise
444: // the text appears to "flash"
445: /* This is causing the text to appear selected when gaining focus. Is this intentionally?
446: * Removed for now.
447: */
448: /*
449: if(enableTextSelection)
450: reviewText.selectAll();
451: enableTextSelection = true;
452: */
453: }
454:
455: public void focusLost(FocusEvent evt) {
456: /* This is causing the text to appear selected when gaining focus. Is this intentionally?
457: * Removed for now.
458: */
459: /*
460: reviewText.setSelectionEnd(0);
461: */
462: }
463: }
|