001: /*
002: * BoxBorderSelectionPanel.java
003: *
004: * Created on March 11, 2008, 1:06 PM
005: */
006:
007: package it.businesslogic.ireport.gui.box;
008:
009: import it.businesslogic.ireport.Box;
010: import it.businesslogic.ireport.ReportElement;
011: import java.awt.BasicStroke;
012: import java.awt.Color;
013: import java.awt.Graphics;
014: import java.awt.Graphics2D;
015: import java.awt.RenderingHints;
016: import java.awt.Stroke;
017: import java.awt.geom.Area;
018: import java.awt.geom.Rectangle2D;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: /**
023: *
024: * @author gtoffoli
025: */
026: public class BoxBorderSelectionPanel extends javax.swing.JPanel {
027:
028: private static final int BORDER_GAP = 15;
029: private static final int REF_SIZE = 8;
030: private static final int REF_DINSTANCE = 4;
031:
032: public void clearSelection() {
033: this .selectedBorders.clear();
034: repaint();
035: fireBorderSelectionChange();
036: }
037:
038: public Box getBox() {
039: return box;
040: }
041:
042: public void setBox(Box box) {
043: this .box = box;
044: }
045:
046: public List<Side> getSelectedBorders() {
047: return selectedBorders;
048: }
049:
050: public void setSelectedBorders(List<Side> selectedBorders) {
051: this .selectedBorders = selectedBorders;
052: }
053:
054: public enum Side {
055: TOP, LEFT, BOTTOM, RIGHT
056: }
057:
058: private List<Side> selectedBorders = new ArrayList<Side>();
059: List<BorderSelectionListener> listeners = new ArrayList<BorderSelectionListener>();
060: private Box box = null;
061:
062: public void addBorderSelectionListener(
063: BorderSelectionListener listener) {
064: if (!listeners.contains(listener))
065: listeners.add(listener);
066: }
067:
068: public void removeBorderSelectionListener(
069: BorderSelectionListener listener) {
070: if (listeners.contains(listener))
071: listeners.remove(listener);
072: }
073:
074: /** Creates new form BoxBorderSelectionPanel */
075: public BoxBorderSelectionPanel() {
076: initComponents();
077:
078: }
079:
080: @Override
081: public void paint(Graphics g) {
082: super .paint(g);
083:
084: Graphics2D g2d = (Graphics2D) g;
085:
086: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
087: RenderingHints.VALUE_ANTIALIAS_ON);
088: g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
089: RenderingHints.VALUE_STROKE_NORMALIZE);
090: g2d.setRenderingHint(RenderingHints.KEY_DITHERING,
091: RenderingHints.VALUE_DITHER_ENABLE);
092:
093: Stroke oldStroke = g2d.getStroke();
094:
095: Stroke defaultStroke = new BasicStroke(1f);
096:
097: // Draw the rectangle, border by border...
098: g2d.setStroke(defaultStroke);
099: g2d.setColor(Color.LIGHT_GRAY);
100:
101: g2d.drawLine(BORDER_GAP - (REF_SIZE + REF_DINSTANCE),
102: BORDER_GAP, BORDER_GAP - REF_DINSTANCE, BORDER_GAP);
103: g2d.drawLine((getWidth() - BORDER_GAP) + REF_DINSTANCE,
104: BORDER_GAP, (getWidth() - BORDER_GAP)
105: + (REF_SIZE + REF_DINSTANCE), BORDER_GAP);
106: g2d.drawLine(BORDER_GAP - (REF_SIZE + REF_DINSTANCE),
107: getHeight() - BORDER_GAP, BORDER_GAP - REF_DINSTANCE,
108: getHeight() - BORDER_GAP);
109: g2d.drawLine((getWidth() - BORDER_GAP) + REF_DINSTANCE,
110: getHeight() - BORDER_GAP, (getWidth() - BORDER_GAP)
111: + (REF_SIZE + REF_DINSTANCE), getHeight()
112: - BORDER_GAP);
113:
114: g2d.drawLine(BORDER_GAP, BORDER_GAP
115: - (REF_SIZE + REF_DINSTANCE), BORDER_GAP, BORDER_GAP
116: - REF_DINSTANCE);
117: g2d.drawLine((getWidth() - BORDER_GAP), BORDER_GAP
118: - REF_DINSTANCE, (getWidth() - BORDER_GAP), BORDER_GAP
119: - (REF_SIZE + REF_DINSTANCE));
120: g2d.drawLine(BORDER_GAP, getHeight() - BORDER_GAP
121: + REF_DINSTANCE, BORDER_GAP, getHeight() - BORDER_GAP
122: + (REF_SIZE + REF_DINSTANCE));
123: g2d.drawLine((getWidth() - BORDER_GAP), getHeight()
124: - BORDER_GAP + REF_DINSTANCE,
125: (getWidth() - BORDER_GAP), getHeight() - BORDER_GAP
126: + (REF_SIZE + REF_DINSTANCE));
127:
128: g2d.setColor(Color.BLACK);
129:
130: boolean defaultPaint = false;
131: Color defaultColor = Color.BLACK;
132: if (getBox() != null && getBox().getPen() != null) {
133: Stroke stroke = ReportElement.getPenStroke("", getBox()
134: .getPen(), 1f);
135: if (stroke != null)
136: defaultStroke = stroke;
137: defaultPaint = getBox().getPen().getLineWidth() != 0;
138: if (getBox().getPen().getLineColor() != null) {
139: defaultColor = getBox().getPen().getLineColor();
140: }
141: }
142:
143: g2d.setStroke(defaultStroke);
144: g2d.setColor(defaultColor);
145:
146: boolean paint = defaultPaint;
147: if (getBox() != null && getBox().getTopPen() != null) {
148: if (getBox().getTopPen().getLineColor() != null)
149: g2d.setColor(getBox().getTopPen().getLineColor());
150: Stroke stroke = ReportElement.getPenStroke("", getBox()
151: .getTopPen(), 1f);
152: if (stroke != null)
153: g2d.setStroke(stroke);
154: paint = getBox().getTopPen().getLineWidth() != 0;
155: }
156: if (paint)
157: g2d.drawLine(BORDER_GAP, BORDER_GAP, this .getWidth()
158: - BORDER_GAP, BORDER_GAP);
159: paint = defaultPaint;
160:
161: // Draw the rectangle, border by border...
162: g2d.setStroke(defaultStroke);
163: g2d.setColor(defaultColor);
164: if (getBox() != null && getBox().getRightPen() != null) {
165: if (getBox().getRightPen().getLineColor() != null)
166: g2d.setColor(getBox().getRightPen().getLineColor());
167: Stroke stroke = ReportElement.getPenStroke("", getBox()
168: .getRightPen(), 1f);
169: if (stroke != null)
170: g2d.setStroke(stroke);
171: paint = getBox().getRightPen().getLineWidth() != 0;
172: }
173: if (paint)
174: g2d.drawLine(this .getWidth() - BORDER_GAP, BORDER_GAP, this
175: .getWidth()
176: - BORDER_GAP, this .getHeight() - BORDER_GAP);
177: paint = defaultPaint;
178:
179: // Draw the rectangle, border by border...
180: g2d.setStroke(defaultStroke);
181: g2d.setColor(defaultColor);
182: if (getBox() != null && getBox().getBottomPen() != null) {
183: if (getBox().getBottomPen().getLineColor() != null)
184: g2d.setColor(getBox().getBottomPen().getLineColor());
185: Stroke stroke = ReportElement.getPenStroke("", getBox()
186: .getBottomPen(), 1f);
187: if (stroke != null)
188: g2d.setStroke(stroke);
189: paint = getBox().getBottomPen().getLineWidth() != 0;
190: }
191:
192: if (paint)
193: g2d.drawLine(BORDER_GAP, this .getHeight() - BORDER_GAP,
194: this .getWidth() - BORDER_GAP, this .getHeight()
195: - BORDER_GAP);
196: paint = defaultPaint;
197:
198: // Draw the rectangle, border by border...
199: g2d.setStroke(defaultStroke);
200: g2d.setColor(defaultColor);
201: if (getBox() != null && getBox().getLeftPen() != null) {
202: if (getBox().getLeftPen().getLineColor() != null)
203: g2d.setColor(getBox().getLeftPen().getLineColor());
204: Stroke stroke = ReportElement.getPenStroke("", getBox()
205: .getLeftPen(), 1f);
206: if (stroke != null)
207: g2d.setStroke(stroke);
208: paint = getBox().getLeftPen().getLineWidth() != 0;
209: }
210: if (paint)
211: g2d.drawLine(BORDER_GAP, BORDER_GAP, BORDER_GAP, this
212: .getHeight()
213: - BORDER_GAP);
214: paint = defaultPaint;
215:
216: // Draw selections...
217: Area a = new Area();
218:
219: Stroke stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT,
220: BasicStroke.JOIN_BEVEL, 0f, new float[] { 1f, 1f }, 0f);
221: g2d.setStroke(stroke);
222: g.setColor(Color.DARK_GRAY);
223: g.setXORMode(Color.WHITE);
224:
225: for (Side s : getSelectedBorders()) {
226: switch (s) {
227: case TOP:
228: Rectangle2D rect1 = new Rectangle2D.Float(
229: BORDER_GAP - 5, BORDER_GAP - 5, getWidth()
230: - (2 * BORDER_GAP) + 10, 10);
231: a.add(new Area(rect1));
232: g2d.draw(rect1);
233: break;
234: case RIGHT:
235: Rectangle2D rect2 = new Rectangle2D.Float(getWidth()
236: - BORDER_GAP - 5, BORDER_GAP - 5, 10,
237: getHeight() - (2 * BORDER_GAP) + 10);
238: a.add(new Area(rect2));
239: g2d.draw(rect2);
240: break;
241: case BOTTOM:
242: Rectangle2D rect3 = new Rectangle2D.Float(
243: BORDER_GAP - 5, getHeight() - (BORDER_GAP) - 5,
244: getWidth() - (2 * BORDER_GAP) + 10, 10);
245: a.add(new Area(rect3));
246: g2d.draw(rect3);
247: break;
248: case LEFT:
249: Rectangle2D rect4 = new Rectangle2D.Float(
250: BORDER_GAP - 5, BORDER_GAP - 5, 10, getHeight()
251: - (2 * BORDER_GAP) + 10);
252: a.add(new Area(rect4));
253: g2d.draw(rect4);
254: break;
255: }
256:
257: Area a2 = new Area(new Rectangle2D.Float(5, 5,
258: getWidth() - 10, getHeight() - 10));
259: a2.intersect(a);
260: //g2d.draw(a2);
261: }
262:
263: g2d.setStroke(oldStroke);
264: }
265:
266: private void fireBorderSelectionChange() {
267: for (BorderSelectionListener listener : listeners) {
268: try {
269: listener.selectionChanged(getSelectedBorders());
270: } catch (Throwable e) {
271: e.printStackTrace();
272: }
273: }
274: }
275:
276: /** This method is called from within the constructor to
277: * initialize the form.
278: * WARNING: Do NOT modify this code. The content of this method is
279: * always regenerated by the Form Editor.
280: */
281: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
282: private void initComponents() {
283:
284: setBackground(new java.awt.Color(255, 255, 255));
285: addMouseListener(new java.awt.event.MouseAdapter() {
286: public void mouseClicked(java.awt.event.MouseEvent evt) {
287: formMouseClicked(evt);
288: }
289:
290: public void mousePressed(java.awt.event.MouseEvent evt) {
291: formMousePressed(evt);
292: }
293: });
294: setLayout(null);
295: }// </editor-fold>//GEN-END:initComponents
296:
297: private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
298:
299: }//GEN-LAST:event_formMouseClicked
300:
301: private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
302: Rectangle2D rect1 = new Rectangle2D.Float(BORDER_GAP - 5,
303: BORDER_GAP - 5, getWidth() - (2 * BORDER_GAP) + 10, 10);
304: Rectangle2D rect2 = new Rectangle2D.Float(getWidth()
305: - BORDER_GAP - 5, BORDER_GAP - 5, 10, getHeight()
306: - (2 * BORDER_GAP) + 10);
307: Rectangle2D rect3 = new Rectangle2D.Float(BORDER_GAP - 5,
308: getHeight() - (BORDER_GAP) - 5, getWidth()
309: - (2 * BORDER_GAP) + 10, 10);
310: Rectangle2D rect4 = new Rectangle2D.Float(BORDER_GAP - 5,
311: BORDER_GAP - 5, 10, getHeight() - (2 * BORDER_GAP) + 10);
312:
313: boolean fireEvent = false;
314:
315: // if (getSelectedBorders().size() > 0 && !evt.isControlDown())
316: // {
317: // getSelectedBorders().clear();
318: // fireEvent = true;
319: // }
320:
321: if (rect1.contains(evt.getPoint())) {
322: if (!selectedBorders.contains(Side.TOP)) {
323: getSelectedBorders().add(Side.TOP);
324: } else {
325: getSelectedBorders().remove(Side.TOP);
326: }
327: fireEvent = true;
328: } else if (rect2.contains(evt.getPoint())) {
329: if (!selectedBorders.contains(Side.RIGHT)) {
330: getSelectedBorders().add(Side.RIGHT);
331: } else {
332: getSelectedBorders().remove(Side.RIGHT);
333: }
334: fireEvent = true;
335: } else if (rect3.contains(evt.getPoint())) {
336: if (!selectedBorders.contains(Side.BOTTOM)) {
337: getSelectedBorders().add(Side.BOTTOM);
338: } else {
339: getSelectedBorders().remove(Side.BOTTOM);
340: }
341: fireEvent = true;
342: } else if (rect4.contains(evt.getPoint())) {
343: if (!selectedBorders.contains(Side.LEFT)) {
344: getSelectedBorders().add(Side.LEFT);
345: } else {
346: getSelectedBorders().remove(Side.LEFT);
347: }
348: fireEvent = true;
349: }
350:
351: repaint();
352: if (fireEvent)
353: fireBorderSelectionChange();
354: }//GEN-LAST:event_formMousePressed
355:
356: // Variables declaration - do not modify//GEN-BEGIN:variables
357: // End of variables declaration//GEN-END:variables
358:
359: }
|