001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JRTextExpressionArea.java
028: *
029: * Created on 28 aprile 2003, 22.55
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import it.businesslogic.ireport.CrosstabReportElement;
036: import it.businesslogic.ireport.ReportElement;
037: import java.awt.event.ActionEvent;
038: import javax.swing.event.DocumentEvent;
039: import javax.swing.event.DocumentListener;
040: import org.syntax.jedit.*;
041: import org.syntax.jedit.tokenmarker.*;
042:
043: import javax.swing.event.UndoableEditEvent;
044: import javax.swing.event.UndoableEditListener;
045:
046: import java.awt.event.MouseWheelEvent;
047: import java.awt.event.MouseWheelListener;
048:
049: import it.businesslogic.ireport.util.I18n;
050: import java.awt.Color;
051: import java.awt.Graphics;
052: import java.awt.Graphics2D;
053: import java.awt.Stroke;
054: import java.util.Vector;
055: import javax.swing.border.Border;
056:
057: /**
058: *
059: * @author Administrator
060: */
061: public class JRTextExpressionArea extends JEditTextArea {
062:
063: private int spessore = 0;
064: protected boolean viewScrollbars = true;
065:
066: private javax.swing.JPopupMenu jPopupMenuTextField;
067: private javax.swing.JMenuItem jMenuItemTextEditor;
068: private javax.swing.JSeparator jSeparator8;
069: private javax.swing.JMenuItem jMenuItemCut;
070: private javax.swing.JMenuItem jMenuItemCopy;
071: private javax.swing.JMenuItem jMenuItemPaste;
072: private javax.swing.JMenuItem jMenuItemSelectAll;
073: private javax.swing.JSeparator jSeparator9;
074: private javax.swing.JMenu jMenuFields;
075: private javax.swing.JMenu jMenuVariables;
076: private javax.swing.JMenu jMenuParameters;
077:
078: private boolean hasErrors = false;
079:
080: private it.businesslogic.ireport.SubDataset subDataset = null;
081: private Vector crosstabElements = new Vector();
082:
083: /** Creates a new instance of JRTextExpressionArea */
084: public JRTextExpressionArea() {
085: super ();
086:
087: setEditButtonVisible(true);
088:
089: painter.setEOLMarkersPainted(false);
090: painter.setInvalidLinesPainted(false);
091: painter.setLineHighlightEnabled(false);
092:
093: spessore = vertical.getWidth();
094: super .painter.setLineHighlightColor(java.awt.Color.WHITE);
095: this .setDocument(new SyntaxDocument());
096:
097: setTokenMarker(new JavaTokenMarker());
098:
099: getDocument().addUndoableEditListener(
100: new SimpleUndoableEditListener());
101: addMouseWheelListener(new SimpleMouseWheelListener());
102: initPopupMenu();
103:
104: setToolTipText("TIP: open the text editor to edit your expression:\npress right mouse button and choose <b>Expression editor</b>");
105:
106: /*
107: this.getInputHandler().addKeyBinding("C+Z", new java.awt.event.ActionListener() {
108:
109: public void actionPerformed(java.awt.event.ActionEvent evt)
110: {
111: undo();
112: ;
113: System.out.println(getName() + " " + getDocument().getUM());
114: }
115: } );
116: */
117:
118: this .getDocument().addDocumentListener(new DocumentListener() {
119: public void changedUpdate(DocumentEvent e) {
120: setHasErrors(false);
121: }
122:
123: public void insertUpdate(DocumentEvent e) {
124: setHasErrors(false);
125: }
126:
127: public void removeUpdate(DocumentEvent e) {
128: setHasErrors(false);
129: }
130: });
131:
132: }
133:
134: public void initPopupMenu() {
135:
136: jPopupMenuTextField = new javax.swing.JPopupMenu();
137: jMenuItemTextEditor = new javax.swing.JMenuItem();
138: jSeparator8 = new javax.swing.JSeparator();
139: jMenuItemCut = new javax.swing.JMenuItem();
140: jMenuItemCopy = new javax.swing.JMenuItem();
141: jMenuItemPaste = new javax.swing.JMenuItem();
142: jMenuItemSelectAll = new javax.swing.JMenuItem();
143: jSeparator9 = new javax.swing.JSeparator();
144: jMenuFields = new javax.swing.JMenu();
145: jMenuVariables = new javax.swing.JMenu();
146: jMenuParameters = new javax.swing.JMenu();
147:
148: jMenuItemTextEditor.setText("Use texteditor");
149: jMenuItemTextEditor
150: .addActionListener(new java.awt.event.ActionListener() {
151: public void actionPerformed(
152: java.awt.event.ActionEvent evt) {
153: jMenuItemTextEditorActionPerformed(evt);
154: }
155: });
156:
157: jPopupMenuTextField.add(jMenuItemTextEditor);
158:
159: jPopupMenuTextField.add(jSeparator8);
160:
161: jMenuItemCut
162: .setIcon(new javax.swing.ImageIcon(
163: getClass()
164: .getResource(
165: "/it/businesslogic/ireport/icons/menu/cut.png")));
166: jMenuItemCut.setText(I18n.getString("cut", "Cut"));
167: //jMenuItemCut.setEnabled(false);
168: jPopupMenuTextField.add(jMenuItemCut);
169:
170: jMenuItemCut
171: .addActionListener(new java.awt.event.ActionListener() {
172: public void actionPerformed(
173: java.awt.event.ActionEvent evt) {
174: jMenuItemCutActionPerformed(evt);
175: }
176: });
177:
178: jMenuItemCopy
179: .setIcon(new javax.swing.ImageIcon(
180: getClass()
181: .getResource(
182: "/it/businesslogic/ireport/icons/menu/copy.png")));
183: jMenuItemCopy.setText(I18n.getString("copy", "Copy"));
184: //jMenuItemCopy.setEnabled(false);
185: jPopupMenuTextField.add(jMenuItemCopy);
186:
187: jMenuItemCopy
188: .addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(
190: java.awt.event.ActionEvent evt) {
191: jMenuItemCopyActionPerformed(evt);
192: }
193: });
194:
195: jMenuItemPaste
196: .setIcon(new javax.swing.ImageIcon(
197: getClass()
198: .getResource(
199: "/it/businesslogic/ireport/icons/menu/paste.png")));
200: jMenuItemPaste.setText(I18n.getString("paste", "Paste"));
201: //jMenuItemPaste.setEnabled(false);
202: jPopupMenuTextField.add(jMenuItemPaste);
203:
204: jMenuItemPaste
205: .addActionListener(new java.awt.event.ActionListener() {
206: public void actionPerformed(
207: java.awt.event.ActionEvent evt) {
208: jMenuItemPasteActionPerformed(evt);
209: }
210: });
211:
212: jMenuItemSelectAll.setText("Select all");
213: //jMenuItemPaste.setEnabled(false);
214: jPopupMenuTextField.add(jMenuItemSelectAll);
215:
216: jMenuItemSelectAll
217: .addActionListener(new java.awt.event.ActionListener() {
218: public void actionPerformed(
219: java.awt.event.ActionEvent evt) {
220: jMenuItemSelectAllActionPerformed(evt);
221: }
222: });
223:
224: /*
225: jPopupMenuTextField.add(jSeparator9);
226:
227: jMenuFields.setText("Fields");
228: jPopupMenuTextField.add(jMenuFields);
229:
230: jMenuVariables.setText("Variables");
231: jPopupMenuTextField.add(jMenuVariables);
232:
233: jMenuParameters.setText("Parameters");
234: jPopupMenuTextField.add(jMenuParameters);
235: */
236:
237: this .setRightClickPopup(jPopupMenuTextField);
238: }
239:
240: private void jMenuItemTextEditorActionPerformed(
241: java.awt.event.ActionEvent evt) {
242: if (MainFrame.getMainInstance().getActiveReportFrame() == null)
243: return;
244: ExpressionEditor ed = new ExpressionEditor();
245:
246: //System.out.println("Sundataset: " + this.getSubDataset());
247: //System.out.println("Crosstabs: " + getCrosstabElements().size());
248:
249: if (this .getSubDataset() == null
250: && getCrosstabElements().size() == 0) {
251: if (MainFrame.getMainInstance().getActiveReportFrame()
252: .getSelectedCrosstabEditorPanel() == null) {
253: this .setSubDataset(MainFrame.getMainInstance()
254: .getActiveReportFrame().getReport());
255: } else {
256: addCrosstab(MainFrame.getMainInstance()
257: .getActiveReportFrame()
258: .getSelectedCrosstabEditorPanel()
259: .getCrosstabElement());
260: }
261: }
262:
263: if (getSubDataset() != null)
264: ed.setSubDataset(this .getSubDataset());
265:
266: ed.setCrosstabElements(getCrosstabElements());
267: ed.setExpression(getText());
268: ed.updateTreeEntries();
269: //System.out.println(getText());
270: ed.setVisible(true);
271: if (ed.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
272: this .setText(ed.getExpression());
273: }
274:
275: }
276:
277: private void jMenuItemCutActionPerformed(
278: java.awt.event.ActionEvent evt) {
279: this .cut();
280: }
281:
282: private void jMenuItemCopyActionPerformed(
283: java.awt.event.ActionEvent evt) {
284: this .copy();
285: }
286:
287: private void jMenuItemPasteActionPerformed(
288: java.awt.event.ActionEvent evt) {
289: this .paste();
290: }
291:
292: private void jMenuItemSelectAllActionPerformed(
293: java.awt.event.ActionEvent evt) {
294: this .setSelectionStart(0);
295: this .setSelectionEnd(getText().length());
296: }
297:
298: /** Getter for property viewScrollbars.
299: * @return Value of property viewScrollbars.
300: *
301: */
302: public boolean isViewScrollbars() {
303: return viewScrollbars;
304: }
305:
306: /** Setter for property viewScrollbars.
307: * @param viewScrollbars New value of property viewScrollbars.
308: *
309: */
310: public void setViewScrollbars(boolean viewScrollbars) {
311:
312: //System.out.println("Ops"+viewScrollbars);
313: //if (this.viewScrollbars == viewScrollbars) return;
314: if (!viewScrollbars) {
315: //super.vertical.setVisible(false);
316: //super.horizontal.setVisible(false);
317:
318: //super.vertical.setSize(0,0);
319: } else {
320: //super.vertical.setVisible(true);
321: //super.horizontal.setVisible(true);
322: //super.vertical.setSize(spessore, super.vertical.getHeight());
323: //super.horizontal.setSize(spessore, super.horizontal.getWidth());
324: }
325: this .viewScrollbars = viewScrollbars;
326: }
327:
328: public void setEnabled(boolean enabled) {
329: if (super .isEnabled() == enabled)
330: return;
331: super .setEnabled(enabled);
332: if (!super .isEnabled()) {
333: for (int i = 0; i < this .getComponentCount(); ++i)
334: this .getComponent(i).setBackground(java.awt.Color.GRAY);
335: this .setCaretVisible(false);
336: this .getPainter().setLineHighlightEnabled(false);
337:
338: } else {
339: for (int i = 0; i < this .getComponentCount(); ++i)
340: this .getComponent(i)
341: .setBackground(java.awt.Color.WHITE);
342: this .setCaretVisible(true);
343: this .getPainter().setLineHighlightEnabled(true);
344: }
345:
346: }
347:
348: class SimpleUndoableEditListener implements UndoableEditListener {
349: public void undoableEditHappened(UndoableEditEvent e) {
350: addEdit(e);
351: //System.out.println("Add to undo.." + getDocument().getUM().canUndo());
352: setHasErrors(false);
353: }
354:
355: protected SimpleUndoableEditListener() {
356: }
357: }
358:
359: class SimpleMouseWheelListener implements MouseWheelListener {
360:
361: public void mouseWheelMoved(MouseWheelEvent e) {
362: if (e.getScrollType() == 0
363: && ((getFirstLine() + getVisibleLines()) - 1)
364: + e.getWheelRotation() < getLineCount()
365: && getFirstLine() + e.getWheelRotation() >= 0)
366: setFirstLine(getFirstLine() + e.getWheelRotation());
367: }
368:
369: protected SimpleMouseWheelListener() {
370: }
371: }
372:
373: public void addEdit(UndoableEditEvent e) {
374: getDocument().getUM().addEdit(e.getEdit());
375: //System.out.println( "Add edit to doc: " + getDocument().getUM() );
376: }
377:
378: public it.businesslogic.ireport.SubDataset getSubDataset() {
379: return subDataset;
380: }
381:
382: public void setSubDataset(
383: it.businesslogic.ireport.SubDataset subDataset) {
384: this .subDataset = subDataset;
385: }
386:
387: public Vector getCrosstabElements() {
388: return crosstabElements;
389: }
390:
391: public void setCrosstabElements(Vector crosstabElements) {
392: this .crosstabElements = crosstabElements;
393: }
394:
395: public void addCrosstab(CrosstabReportElement cr) {
396: this .getCrosstabElements().add(cr);
397:
398: }
399:
400: public void editButtonActionPerformed(ActionEvent e) {
401: jMenuItemTextEditorActionPerformed(e);
402: }
403:
404: public void applyI18n() {
405: // Start autogenerated code ----------------------
406: jMenuItemTextEditor.setText(I18n.getString(
407: "jRTextExpressionArea.menuItemTextEditor",
408: "Use texteditor"));
409: jMenuItemSelectAll
410: .setText(I18n.getString(
411: "jRTextExpressionArea.menuItemSelectAll",
412: "Select all"));
413: jMenuFields.setText(I18n.getString(
414: "jRTextExpressionArea.menuFields", "Fields"));
415: jMenuVariables.setText(I18n.getString(
416: "jRTextExpressionArea.menuVariables", "Variables"));
417: jMenuParameters.setText(I18n.getString(
418: "jRTextExpressionArea.menuParameters", "Parameters"));
419: // End autogenerated code ----------------------
420: }
421:
422: public Border oldBorder = null;
423:
424: public void paint(Graphics graphics) {
425: super .paint(graphics);
426:
427: if (isHasErrors()) {
428: Graphics2D g = (Graphics2D) graphics;
429: Stroke s = g.getStroke();
430: g
431: .setStroke(ReportElement.getPenStroke("4Point",
432: null, 1.0));
433: g.setColor(ReportElement.getAlphaColor(Color.RED, 128));
434: g.drawRect(0, 0, getWidth(), getHeight());
435: g.setStroke(s);
436: }
437: }
438:
439: public boolean isHasErrors() {
440: return hasErrors;
441: }
442:
443: public void setHasErrors(boolean hasErrors) {
444: this.hasErrors = hasErrors;
445: this.repaint();
446: }
447:
448: }
|