001: package Schmortopf.ProjectFiles.ProjectFilesTree.SubComponents;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.border.*;
007: import javax.swing.event.*;
008: import javax.swing.text.*;
009:
010: import Schmortopf.Main.IDE_ProjectFrameProvider;
011: import Schmortopf.Utility.gui.JContrastLabel;
012: import Schmortopf.Utility.gui.JSenseButton;
013: import Schmortopf.Utility.gui.JSenseToggleButton;
014: import Schmortopf.Utility.gui.EFCNGradientPanel;
015: import Schmortopf.JavaSourceEditor.JEditorTextPane;
016: import Schmortopf.JavaSourceEditor.SourceEditorDocument;
017: import Language.Language;
018: import Shared.Logging.Log;
019:
020: public class ProjectFilesNewFileDialog extends JDialog {
021:
022: private JTextField inputTextField;
023: private JCheckBox addStaticMainMethodCheckBox;
024: private boolean wasOKButtonPressed = false;
025:
026: private String schmortopfMainDirectory;
027:
028: private ProjectFilesXMLTemplate[] xmlTemplates = null;
029: private JSenseToggleButton[] templateButtons = null;
030:
031: private String packageLine;
032:
033: private JButton okButton;
034: private JButton cancelButton;
035: private JButton previewButton;
036:
037: public ProjectFilesNewFileDialog(Dialog owner,
038: final String schmortopfMainDirectory, Point centerPoint,
039: String dialogTitle, String title, String inputMessage,
040: final String packageLine,
041: final IDE_ProjectFrameProvider projectFrameProvider) {
042: super (owner, dialogTitle, true);
043: this .schmortopfMainDirectory = schmortopfMainDirectory;
044: this .packageLine = packageLine;
045: this .furtherConstruction(centerPoint, dialogTitle, title,
046: inputMessage, projectFrameProvider);
047: }
048:
049: public ProjectFilesNewFileDialog(JFrame owner,
050: final String schmortopfMainDirectory, Point centerPoint,
051: String dialogTitle, String title, String inputMessage,
052: final String packageLine,
053: final IDE_ProjectFrameProvider projectFrameProvider) {
054: super (owner, dialogTitle, true);
055: this .schmortopfMainDirectory = schmortopfMainDirectory;
056: this .packageLine = packageLine;
057: this .furtherConstruction(centerPoint, dialogTitle, title,
058: inputMessage, projectFrameProvider);
059: }
060:
061: private void furtherConstruction(Point centerPoint,
062: String dialogTitle, String title, String inputMessage,
063: final IDE_ProjectFrameProvider projectFrameProvider) {
064: this .getContentPane().setLayout(new BorderLayout());
065:
066: final int fontSize = UIManager.getFont("Label.font").getSize();
067:
068: // message parts :
069: JPanel mainPanel = new JPanel(new BorderLayout(fontSize,
070: fontSize));
071: mainPanel.setBorder(BorderFactory.createEmptyBorder(fontSize,
072: fontSize, fontSize, fontSize));
073:
074: JPanel upperPanel = new JPanel(new BorderLayout(fontSize,
075: fontSize));
076: upperPanel.setBorder(BorderFactory
077: .createEmptyBorder(0, 0, 0, 0));
078: mainPanel.add(upperPanel, BorderLayout.NORTH);
079:
080: JTextField titleTextField = new JTextField(title);
081: titleTextField.setEditable(false);
082: titleTextField.setFont(UIManager.getFont("Label.font"));
083: Border ib = BorderFactory.createEmptyBorder(fontSize / 2,
084: fontSize, fontSize / 2, fontSize / 2);
085: Border ob = BorderFactory.createLineBorder(this
086: .getSlightlyLightColor());
087: titleTextField.setBorder(BorderFactory.createCompoundBorder(ob,
088: ib));
089: upperPanel.add(titleTextField, BorderLayout.NORTH);
090:
091: JLabel msgLabel = new JContrastLabel(inputMessage);
092: this .inputTextField = new JTextField(new IdentifierDocument(),
093: "", 32);
094: this .inputTextField
095: .addActionListener(new java.awt.event.ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: okButtonPressed();
098: }
099: });
100: // Enable/disable the buttons, if the text in the inputTextField
101: // is valid/invalid:
102: DocumentListener myListener = new DocumentListener() {
103: public void insertUpdate(DocumentEvent e) {
104: checkInputText();
105: }
106:
107: public void removeUpdate(DocumentEvent e) {
108: checkInputText();
109: }
110:
111: public void changedUpdate(DocumentEvent e) {
112: checkInputText();
113: }
114: };
115: this .inputTextField.getDocument().addDocumentListener(
116: myListener);
117:
118: this .addStaticMainMethodCheckBox = new JCheckBox(Language
119: .Translate("Add a static main() method"), false);
120:
121: JPanel inputTextPanel = new JPanel(new FlowLayout(
122: FlowLayout.LEFT, 0, fontSize / 2));
123: inputTextPanel.setBorder(BorderFactory.createEmptyBorder(0, 0,
124: 0, 0));
125: inputTextPanel.add(msgLabel);
126: inputTextPanel.add(this .inputTextField);
127:
128: JPanel inputPanel = new JPanel(new BorderLayout(0, 0));
129: ib = BorderFactory.createEmptyBorder(fontSize / 2, fontSize,
130: fontSize / 2, fontSize / 2);
131: ob = BorderFactory.createLineBorder(this
132: .getSlightlyLightColor());
133: inputPanel
134: .setBorder(BorderFactory.createCompoundBorder(ob, ib));
135:
136: inputPanel.add(inputTextPanel, BorderLayout.CENTER);
137: inputPanel.add(this .addStaticMainMethodCheckBox,
138: BorderLayout.SOUTH);
139:
140: upperPanel.add(inputPanel, BorderLayout.CENTER);
141:
142: // Create and add the file templates panel :
143: mainPanel.add(new JScrollPane(this .createTheTemplatesPanel(
144: fontSize, projectFrameProvider)), BorderLayout.CENTER);
145:
146: // button parts :
147: this .okButton = new JSenseButton(
148: Language.Translate(" Ok "), true, true,
149: projectFrameProvider.getMainFrameProvider());
150: this .okButton.setIcon(projectFrameProvider
151: .loadImageIcon("pics/menupics/ok.gif"));
152: this .okButton.setFocusPainted(false);
153: this .okButton
154: .addActionListener(new java.awt.event.ActionListener() {
155: public void actionPerformed(ActionEvent e) {
156: okButtonPressed();
157: }
158: });
159: this .okButton.setEnabled(false); // enabled by checkInputText(), if the inputtextfield text is ok.
160:
161: this .cancelButton = new JSenseButton(Language
162: .Translate(" Cancel "), true, true,
163: projectFrameProvider.getMainFrameProvider());
164: this .cancelButton.setIcon(projectFrameProvider
165: .loadImageIcon("pics/menupics/cancel.gif"));
166: this .cancelButton.setFocusPainted(false);
167: this .cancelButton
168: .addActionListener(new java.awt.event.ActionListener() {
169: public void actionPerformed(ActionEvent e) {
170: setVisible(false);
171: }
172: });
173:
174: this .previewButton = new JSenseButton(Language
175: .Translate(" Preview "), true, true,
176: projectFrameProvider.getMainFrameProvider());
177: this .previewButton.setIcon(projectFrameProvider
178: .loadImageIcon("pics/menupics/preview.gif"));
179: this .previewButton.setFocusPainted(false);
180: this .previewButton
181: .addActionListener(new java.awt.event.ActionListener() {
182: public void actionPerformed(ActionEvent e) {
183: showPreview(fontSize, projectFrameProvider);
184: }
185: });
186: this .previewButton.setEnabled(false); // enabled by checkInputText(), if the inputtextfield text is ok.
187:
188: JPanel buttonPanel = new EFCNGradientPanel(new FlowLayout(),
189: EFCNGradientPanel.ApplyVerticalHighLight,
190: EFCNGradientPanel.StrongGradientStrength,
191: EFCNGradientPanel.PanelBackground);
192: buttonPanel.add(okButton);
193: buttonPanel.add(cancelButton);
194: buttonPanel.add(previewButton);
195:
196: // Set a horizontal strut at the top, which holds the dialogs width
197: // at a minimum :
198: this .getContentPane().add(
199: Box.createHorizontalStrut(46 * fontSize),
200: BorderLayout.NORTH);
201:
202: this .getContentPane().add(mainPanel, BorderLayout.CENTER);
203: this .getContentPane().add(buttonPanel, BorderLayout.SOUTH);
204:
205: // fixed, but fontsize relative size :
206: this .setSize(fontSize * 48, fontSize * 36);
207:
208: this .setLocation(centerPoint.x - this .getWidth() / 2,
209: centerPoint.y - this .getHeight() / 2);
210:
211: } // furtherConstruction
212:
213: /**
214: * Used for the texfield to limit characters to be part of java identifiers only.
215: */
216: class IdentifierDocument extends PlainDocument {
217: public void insertString(int offs, String str, AttributeSet a)
218: throws BadLocationException {
219: if (str == null)
220: return;
221: String identifierString = "";
222: for (int i = 0; i < str.length(); i++) {
223: char c = str.charAt(i);
224: if ((offs == 0) && (i == 0)) {
225: if (Character.isJavaIdentifierStart(c)) {
226: identifierString += "" + c;
227: }
228: } else if (Character.isJavaIdentifierPart(c)) {
229: identifierString += "" + c;
230: }
231: }
232: super .insertString(offs, identifierString, a);
233: }
234: }
235:
236: /**
237: * Called after inputs in the textfield.
238: * Sets the buttons enabled or disabled depending on the text.
239: */
240: private void checkInputText() {
241: if (this .inputTextField.getText().length() > 0) {
242: this .okButton.setEnabled(true);
243: this .previewButton.setEnabled(true);
244: } else {
245: this .okButton.setEnabled(false);
246: this .previewButton.setEnabled(false);
247: }
248: }
249:
250: private JPanel createTheTemplatesPanel(int fontSize,
251: final IDE_ProjectFrameProvider projectFrameProvider) {
252: JPanel templatesPanel = new JPanel(new GridLayout(0, 4));
253: templatesPanel
254: .setBorder(BorderFactory.createEmptyBorder(
255: fontSize / 2, fontSize / 2, fontSize / 2,
256: fontSize / 2));
257: // Create the xml template reader :
258: ProjectFilesXMLReader xmlReader = new ProjectFilesXMLReader(
259: projectFrameProvider);
260:
261: this .xmlTemplates = xmlReader.getParsedXMLTemplates();
262:
263: //ystem.out.println("ProjectFilesNewFileDialog.createTheTemplatesPanel(): " +
264: // xmlTemplates.length + " xml templates created." );
265:
266: this .templateButtons = new JSenseToggleButton[xmlTemplates.length];
267:
268: for (int fileIndex = 0; fileIndex < xmlTemplates.length; fileIndex++) {
269: //ystem.out.println("ProjectFilesNewFileDialog.createTheTemplatesPanel(): template: " +
270: // xmlTemplates[fileIndex].getSourceFilePath() );
271:
272: // Take the level 1 elements: (The root is the "CLASS" tag)
273: ProjectFilesXMLTag rootTag = xmlTemplates[fileIndex]
274: .getRootTag();
275: ProjectFilesXMLTag[] levelOneChildrenTags = rootTag
276: .getChildTags();
277:
278: String templateName = rootTag
279: .getChildContentForTagIdentifier("TEMPLATENAME");
280: String templateDescription = rootTag
281: .getChildContentForTagIdentifier("TEMPLATEDESCRIPTION");
282: String relativeIconPath = rootTag
283: .getChildContentForTagIdentifier("TEMPLATEICON");
284: String sourceFilePath = xmlTemplates[fileIndex]
285: .getSourceFilePath();
286: if ((templateName.length() > 0)
287: && (relativeIconPath.length() > 0)) {
288: Icon templateIcon = projectFrameProvider
289: .loadImageIcon(relativeIconPath);
290: JSenseToggleButton templateButton = new JSenseToggleButton(
291: "", templateIcon, true);
292: templateButtons[fileIndex] = templateButton;
293: templateButton.setToolTipText(templateDescription);
294: // radio button behaviour :
295: final int finalIndex = fileIndex;
296: templateButton
297: .addMouseListener(new RadioButtonMouseAdapter(
298: templateButtons, finalIndex));
299:
300: // Catch the "default.xml" - We select this one as default.
301: // We have to initially select or unselect all buttons, otherwise
302: // we had some initial gui side effects. (cause the togglebuttons
303: // are designed somewhat lazily..)
304: if (sourceFilePath.endsWith("default.xml")) {
305: templateButton.setSelected(true);
306: } else {
307: templateButton.setSelected(false);
308: }
309:
310: JPanel singleTemplatePanel = new JPanel(
311: new BorderLayout(0, 0));
312: singleTemplatePanel.setBorder(BorderFactory
313: .createEmptyBorder(fontSize / 8, fontSize / 8,
314: fontSize / 2, fontSize / 8));
315: // For UI space reasons, we must limit the name length to 22 characters :
316: String uiName = templateName;
317: if (uiName.length() > 22)
318: uiName = uiName.substring(0, 20) + "..";
319: JContrastLabel nameLabel = new JContrastLabel(uiName);
320: nameLabel.setFont(UIManager.getFont("Tree.font"));
321: singleTemplatePanel.add(templateButton,
322: BorderLayout.CENTER);
323: JPanel nameLabelPanel = new JPanel(); // centers horizontally
324: nameLabelPanel.add(nameLabel);
325: singleTemplatePanel.add(nameLabelPanel,
326: BorderLayout.SOUTH);
327: templatesPanel.add(singleTemplatePanel);
328: } else {
329: templateButtons[fileIndex] = null;
330: if (templateName.length() == 0)
331: Log
332: .Error("TEMPLATENAME incorrect or missing in file "
333: + xmlTemplates[fileIndex]
334: .getSourceFilePath());
335: if (relativeIconPath.length() == 0)
336: Log
337: .Error("TEMPLATEICON incorrect or missing in file "
338: + xmlTemplates[fileIndex]
339: .getSourceFilePath());
340: }
341:
342: } // for fileIndex
343:
344: // decouple from BorderLayout CENTER :
345: JPanel templateWrapPanel = new JPanel(new BorderLayout(0, 0));
346: templateWrapPanel.add(templatesPanel, BorderLayout.NORTH);
347: return templateWrapPanel;
348: } // createTheTemplatesPanel
349:
350: /**
351: * Shows a preview with the currently selected template
352: * as child of this dialog, without closing this dialog.
353: */
354: private void showPreview(final int fontSize,
355: final IDE_ProjectFrameProvider projectFrameProvider) {
356: ProjectFilesXMLTemplate selectedXMLTemplate = this
357: .getSelectedXMLTemplate();
358: String templateName = selectedXMLTemplate.getRootTag()
359: .getChildContentForTagIdentifier("TEMPLATENAME");
360: String templateText = this .getSelectedTemplateText();
361:
362: final JDialog preview = new JDialog(this , Language.Translate(
363: "Preview of %", templateName));
364: preview.getContentPane().setLayout(new BorderLayout());
365:
366: JTextArea textArea = new JTextArea(templateText);
367: textArea.setEditable(false);
368:
369: JPanel mainPanel = new JPanel(new BorderLayout(0, 0));
370: mainPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
371:
372: // button parts :
373: JButton okButton = new JSenseButton(Language
374: .Translate(" Ok "), true, true,
375: projectFrameProvider.getMainFrameProvider());
376: okButton.setIcon(projectFrameProvider
377: .loadImageIcon("pics/menupics/ok.gif"));
378: okButton.setFocusPainted(false);
379: okButton.addActionListener(new java.awt.event.ActionListener() {
380: public void actionPerformed(ActionEvent e) {
381: preview.setVisible(false);
382: }
383: });
384: JPanel buttonPanel = new JPanel(new FlowLayout());
385: buttonPanel.add(okButton);
386: mainPanel.add(buttonPanel, BorderLayout.SOUTH);
387:
388: preview.getContentPane().add(mainPanel, BorderLayout.CENTER);
389:
390: // Set location and size based on this one :
391: Point loc = this .getLocation();
392: preview.setLocation(loc.x + 2 * fontSize, loc.y + 2 * fontSize);
393: preview.setSize(this .getSize());
394: preview.setVisible(true);
395:
396: textArea.setCaretPosition(0);
397:
398: } // showPreview
399:
400: /**
401: * Returns the selected template text of the dialog. This also is
402: * valid, after the dialog has been closed.
403: */
404: public String getSelectedTemplateText() {
405: String templateText = "";
406: ProjectFilesXMLTemplate selectedXMLTemplate = this
407: .getSelectedXMLTemplate();
408: if (selectedXMLTemplate != null) {
409: String className = this .inputTextField.getText();
410: boolean doAddMainMethod = this .addStaticMainMethodCheckBox
411: .isSelected();
412: templateText = ProjectFilesXMLUtilities.ParseXMLTemplate(
413: selectedXMLTemplate, className, this .packageLine,
414: doAddMainMethod);
415: }
416: return templateText;
417: }
418:
419: /**
420: * After the dialog has been constructed, on can get the
421: * selected xml template here.
422: */
423: public ProjectFilesXMLTemplate getSelectedXMLTemplate() {
424: ProjectFilesXMLTemplate selectedXMLTemplate = null;
425: int selectedIndex = -1;
426: if (this .templateButtons != null) {
427: for (int i = 0; i < this .templateButtons.length; i++) {
428: if (this .templateButtons[i].isSelected()) {
429: selectedIndex = i;
430: }
431: } // for
432: } // if
433: if ((selectedIndex >= 0) && (this .xmlTemplates != null)) {
434: selectedXMLTemplate = this .xmlTemplates[selectedIndex];
435: }
436: return selectedXMLTemplate;
437: } // getSelectedXMLTemplate
438:
439: class RadioButtonMouseAdapter extends MouseAdapter {
440:
441: private JButton[] buttonArray;
442: private int buttonIndex;
443:
444: public RadioButtonMouseAdapter(final JButton[] buttonArray,
445: final int buttonIndex) {
446: this .buttonArray = buttonArray;
447: this .buttonIndex = buttonIndex;
448: }
449:
450: public void mouseReleased(MouseEvent e) {
451: for (int i = 0; i < this .buttonArray.length; i++) {
452: if (this .buttonArray[i] != null) {
453: if (i != this .buttonIndex) {
454: this .buttonArray[i].setSelected(false);
455: }
456: }
457: }
458: // If none is selected now, select the firt one :
459: boolean selectedOneFound = false;
460: for (int i = 0; i < this .buttonArray.length; i++) {
461: if (this .buttonArray[i].isSelected()) {
462: selectedOneFound = true;
463: break;
464: }
465: }
466: if (!selectedOneFound) {
467: this .buttonArray[0].setSelected(true);
468: }
469: }
470: } // RadioButtonMouseAdapter
471:
472: public String getInputFieldText() {
473: return this .inputTextField.getText().trim();
474: }
475:
476: public void setInputFieldText(String theText) {
477: this .inputTextField.setText(theText);
478: }
479:
480: public void okButtonPressed() {
481: this .wasOKButtonPressed = true;
482: this .setVisible(false);
483: } // okButtonPressed
484:
485: public boolean getWasOkButtonPressed() {
486: return this .wasOKButtonPressed;
487: }
488:
489: private Color getSlightlyLightColor() {
490: Color c1 = UIManager.getColor("Label.background");
491: Color c2 = UIManager.getColor("Tree.background");
492: int r = (c1.getRed() + c2.getRed()) / 2;
493: int g = (c1.getGreen() + c2.getGreen()) / 2;
494: int b = (c1.getBlue() + c2.getBlue()) / 2;
495: return new Color(r, g, b);
496: } // getSlightlyLightColor
497:
498: } // ProjectFilesNewFileDialog
|