001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.contrib.view;
019:
020: import java.awt.*;
021:
022: import javax.swing.border.AbstractBorder;
023:
024: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
025:
026: /**
027: * This is an attempt to implement Excel style borders for the SheetViewer.
028: * Mostly just overrides stuff so the javadoc won't appear here but will
029: * appear in the generated stuff.
030: *
031: * @author Andrew C. Oliver (acoliver at apache dot org)
032: * @author Jason Height
033: */
034: public class SVBorder extends AbstractBorder {
035: private Color northColor = null;
036: private Color eastColor = null;
037: private Color southColor = null;
038: private Color westColor = null;
039: private int northBorderType = HSSFCellStyle.BORDER_NONE;
040: private int eastBorderType = HSSFCellStyle.BORDER_NONE;
041: private int southBorderType = HSSFCellStyle.BORDER_NONE;
042: private int westBorderType = HSSFCellStyle.BORDER_NONE;
043: private boolean northBorder = false;
044: private boolean eastBorder = false;
045: private boolean southBorder = false;
046: private boolean westBorder = false;
047: private boolean selected = false;
048:
049: public void setBorder(Color northColor, Color eastColor,
050: Color southColor, Color westColor, int northBorderType,
051: int eastBorderType, int southBorderType,
052: int westBorderType, boolean selected) {
053: this .eastColor = eastColor;
054: this .southColor = southColor;
055: this .westColor = westColor;
056: this .northBorderType = northBorderType;
057: this .eastBorderType = eastBorderType;
058: this .southBorderType = southBorderType;
059: this .westBorderType = westBorderType;
060: this .northBorder = northBorderType != HSSFCellStyle.BORDER_NONE;
061: this .eastBorder = eastBorderType != HSSFCellStyle.BORDER_NONE;
062: this .southBorder = southBorderType != HSSFCellStyle.BORDER_NONE;
063: this .westBorder = westBorderType != HSSFCellStyle.BORDER_NONE;
064: this .selected = selected;
065: }
066:
067: public void paintBorder(Component c, Graphics g, int x, int y,
068: int width, int height) {
069: Color oldColor = g.getColor();
070:
071: paintSelectedBorder(g, x, y, width, height);
072: paintNormalBorders(g, x, y, width, height);
073: paintDottedBorders(g, x, y, width, height);
074: paintDashedBorders(g, x, y, width, height);
075: paintDoubleBorders(g, x, y, width, height);
076: paintDashDotDotBorders(g, x, y, width, height);
077:
078: g.setColor(oldColor);
079: }
080:
081: /**
082: * Called by paintBorder to paint the border of a selected cell.
083: * The paramaters are the Graphics object, location and dimensions of the
084: * cell.
085: */
086: private void paintSelectedBorder(Graphics g, int x, int y,
087: int width, int height) {
088: if (selected) {
089: //Need to setup thickness of 2
090: g.setColor(Color.black);
091: //paint the border
092: g.drawRect(x, y, width - 1, height - 1);
093:
094: //paint the filled rectangle at the bottom left hand position
095: g.fillRect(x + width - 5, y + height - 5, 5, 5);
096: }
097: }
098:
099: /**
100: * Called by paintBorder to paint the various versions of normal line
101: * borders for a cell.
102: */
103: private void paintNormalBorders(Graphics g, int x, int y,
104: int width, int height) {
105:
106: if (northBorder
107: && ((northBorderType == HSSFCellStyle.BORDER_THIN)
108: || (northBorderType == HSSFCellStyle.BORDER_MEDIUM) || (northBorderType == HSSFCellStyle.BORDER_THICK))) {
109:
110: int thickness = getThickness(northBorderType);
111:
112: g.setColor(northColor);
113:
114: for (int k = 0; k < thickness; k++) {
115: g.drawLine(x, y + k, width, y + k);
116: }
117: }
118:
119: if (eastBorder
120: && ((eastBorderType == HSSFCellStyle.BORDER_THIN)
121: || (eastBorderType == HSSFCellStyle.BORDER_MEDIUM) || (eastBorderType == HSSFCellStyle.BORDER_THICK))) {
122:
123: int thickness = getThickness(eastBorderType);
124:
125: g.setColor(eastColor);
126:
127: for (int k = 0; k < thickness; k++) {
128: g.drawLine(width - k, y, width - k, height);
129: }
130: }
131:
132: if (southBorder
133: && ((southBorderType == HSSFCellStyle.BORDER_THIN)
134: || (southBorderType == HSSFCellStyle.BORDER_MEDIUM) || (southBorderType == HSSFCellStyle.BORDER_THICK))) {
135:
136: int thickness = getThickness(southBorderType);
137:
138: g.setColor(southColor);
139: for (int k = 0; k < thickness; k++) {
140: g.drawLine(x, height - k, width, height - k);
141: }
142: }
143:
144: if (westBorder
145: && ((westBorderType == HSSFCellStyle.BORDER_THIN)
146: || (westBorderType == HSSFCellStyle.BORDER_MEDIUM) || (westBorderType == HSSFCellStyle.BORDER_THICK))) {
147:
148: int thickness = getThickness(westBorderType);
149:
150: g.setColor(westColor);
151:
152: for (int k = 0; k < thickness; k++) {
153: g.drawLine(x + k, y, x + k, height);
154: }
155: }
156: }
157:
158: /**
159: * Called by paintBorder to paint the dotted line
160: * borders for a cell.
161: */
162: private void paintDottedBorders(Graphics g, int x, int y,
163: int width, int height) {
164: if (northBorder
165: && northBorderType == HSSFCellStyle.BORDER_DOTTED) {
166: int thickness = getThickness(northBorderType);
167:
168: g.setColor(northColor);
169:
170: for (int k = 0; k < thickness; k++) {
171: for (int xc = x; xc < width; xc = xc + 2) {
172: g.drawLine(xc, y + k, xc, y + k);
173: }
174: }
175: }
176:
177: if (eastBorder && eastBorderType == HSSFCellStyle.BORDER_DOTTED) {
178:
179: int thickness = getThickness(eastBorderType);
180: thickness++; //need for dotted borders to show up east
181:
182: g.setColor(eastColor);
183:
184: for (int k = 0; k < thickness; k++) {
185: for (int yc = y; yc < height; yc = yc + 2) {
186: g.drawLine(width - k, yc, width - k, yc);
187: }
188: }
189: }
190:
191: if (southBorder
192: && southBorderType == HSSFCellStyle.BORDER_DOTTED) {
193:
194: int thickness = getThickness(southBorderType);
195: thickness++;
196: g.setColor(southColor);
197: for (int k = 0; k < thickness; k++) {
198: for (int xc = x; xc < width; xc = xc + 2) {
199: g.drawLine(xc, height - k, xc, height - k);
200: }
201: }
202: }
203:
204: if (westBorder && westBorderType == HSSFCellStyle.BORDER_DOTTED) {
205:
206: int thickness = getThickness(westBorderType);
207: // thickness++;
208:
209: g.setColor(westColor);
210:
211: for (int k = 0; k < thickness; k++) {
212: for (int yc = y; yc < height; yc = yc + 2) {
213: g.drawLine(x + k, yc, x + k, yc);
214: }
215: }
216: }
217: }
218:
219: /**
220: * Called by paintBorder to paint the various versions of dotted line
221: * borders for a cell.
222: */
223: private void paintDashedBorders(Graphics g, int x, int y,
224: int width, int height) {
225: if (northBorder
226: && ((northBorderType == HSSFCellStyle.BORDER_DASHED) || (northBorderType == HSSFCellStyle.BORDER_HAIR))) {
227: int thickness = getThickness(northBorderType);
228:
229: int dashlength = 1;
230:
231: if (northBorderType == HSSFCellStyle.BORDER_DASHED)
232: dashlength = 2;
233:
234: g.setColor(northColor);
235:
236: for (int k = 0; k < thickness; k++) {
237: for (int xc = x; xc < width; xc = xc + 5) {
238: g.drawLine(xc, y + k, xc + dashlength, y + k);
239: }
240: }
241: }
242:
243: if (eastBorder
244: && ((eastBorderType == HSSFCellStyle.BORDER_DASHED) || (eastBorderType == HSSFCellStyle.BORDER_HAIR))) {
245:
246: int thickness = getThickness(eastBorderType);
247: thickness++; //need for dotted borders to show up east
248:
249: int dashlength = 1;
250:
251: if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
252: dashlength = 2;
253:
254: g.setColor(eastColor);
255:
256: for (int k = 0; k < thickness; k++) {
257: for (int yc = y; yc < height; yc = yc + 5) {
258: g.drawLine(width - k, yc, width - k, yc
259: + dashlength);
260: }
261: }
262: }
263:
264: if (southBorder
265: && ((southBorderType == HSSFCellStyle.BORDER_DASHED) || (southBorderType == HSSFCellStyle.BORDER_HAIR))) {
266:
267: int thickness = getThickness(southBorderType);
268: thickness++;
269:
270: int dashlength = 1;
271:
272: if (southBorderType == HSSFCellStyle.BORDER_DASHED)
273: dashlength = 2;
274:
275: g.setColor(southColor);
276: for (int k = 0; k < thickness; k++) {
277: for (int xc = x; xc < width; xc = xc + 5) {
278: g.drawLine(xc, height - k, xc + dashlength, height
279: - k);
280: }
281: }
282: }
283:
284: if (westBorder
285: && ((westBorderType == HSSFCellStyle.BORDER_DASHED) || (westBorderType == HSSFCellStyle.BORDER_HAIR))) {
286:
287: int thickness = getThickness(westBorderType);
288: // thickness++;
289:
290: int dashlength = 1;
291:
292: if (westBorderType == HSSFCellStyle.BORDER_DASHED)
293: dashlength = 2;
294:
295: g.setColor(westColor);
296:
297: for (int k = 0; k < thickness; k++) {
298: for (int yc = y; yc < height; yc = yc + 5) {
299: g.drawLine(x + k, yc, x + k, yc + dashlength);
300: }
301: }
302: }
303: }
304:
305: /**
306: * Called by paintBorder to paint the double line
307: * borders for a cell.
308: */
309: private void paintDoubleBorders(Graphics g, int x, int y,
310: int width, int height) {
311: if (northBorder
312: && northBorderType == HSSFCellStyle.BORDER_DOUBLE) {
313:
314: g.setColor(northColor);
315:
316: int leftx = x;
317: int rightx = width;
318:
319: // if there are borders on the west or east then
320: // the second line shouldn't cross them
321: if (westBorder)
322: leftx = x + 3;
323:
324: if (eastBorder)
325: rightx = width - 3;
326:
327: g.drawLine(x, y, width, y);
328: g.drawLine(leftx, y + 2, rightx, y + 2);
329: }
330:
331: if (eastBorder && eastBorderType == HSSFCellStyle.BORDER_DOUBLE) {
332:
333: int thickness = getThickness(eastBorderType);
334: thickness++; //need for dotted borders to show up east
335:
336: g.setColor(eastColor);
337:
338: int topy = y;
339: int bottomy = height;
340:
341: if (northBorder)
342: topy = y + 3;
343:
344: if (southBorder)
345: bottomy = height - 3;
346:
347: g.drawLine(width - 1, y, width - 1, height);
348: g.drawLine(width - 3, topy, width - 3, bottomy);
349: }
350:
351: if (southBorder
352: && southBorderType == HSSFCellStyle.BORDER_DOUBLE) {
353:
354: g.setColor(southColor);
355:
356: int leftx = y;
357: int rightx = width;
358:
359: if (westBorder)
360: leftx = x + 3;
361:
362: if (eastBorder)
363: rightx = width - 3;
364:
365: g.drawLine(x, height - 1, width, height - 1);
366: g.drawLine(leftx, height - 3, rightx, height - 3);
367: }
368:
369: if (westBorder && westBorderType == HSSFCellStyle.BORDER_DOUBLE) {
370:
371: int thickness = getThickness(westBorderType);
372: // thickness++;
373:
374: g.setColor(westColor);
375:
376: int topy = y;
377: int bottomy = height - 3;
378:
379: if (northBorder)
380: topy = y + 2;
381:
382: if (southBorder)
383: bottomy = height - 3;
384:
385: g.drawLine(x, y, x, height);
386: g.drawLine(x + 2, topy, x + 2, bottomy);
387: }
388: }
389:
390: /**
391: * Called by paintBorder to paint the various versions of dash dot dot line
392: * borders for a cell.
393: */
394: private void paintDashDotDotBorders(Graphics g, int x, int y,
395: int width, int height) {
396: if (northBorder
397: && ((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || (northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))) {
398: int thickness = getThickness(northBorderType);
399:
400: g.setColor(northColor);
401: for (int l = x; l < width;) {
402: l = l + drawDashDotDot(g, l, y, thickness, true, true);
403: }
404:
405: }
406:
407: if (eastBorder
408: && ((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || (eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))) {
409:
410: int thickness = getThickness(eastBorderType);
411:
412: g.setColor(eastColor);
413:
414: for (int l = y; l < height;) {
415: //System.err.println("drawing east");
416: l = l
417: + drawDashDotDot(g, width - 1, l, thickness,
418: false, false);
419: }
420: }
421:
422: if (southBorder
423: && ((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || (southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))) {
424:
425: int thickness = getThickness(southBorderType);
426:
427: g.setColor(southColor);
428:
429: for (int l = x; l < width;) {
430: //System.err.println("drawing south");
431: l = l
432: + drawDashDotDot(g, l, height - 1, thickness,
433: true, false);
434: }
435: }
436:
437: if (westBorder
438: && ((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || (westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))) {
439:
440: int thickness = getThickness(westBorderType);
441:
442: g.setColor(westColor);
443:
444: for (int l = y; l < height;) {
445: //System.err.println("drawing west");
446: l = l + drawDashDotDot(g, x, l, thickness, false, true);
447: }
448:
449: }
450: }
451:
452: /**
453: * Draws one dash dot dot horizontally or vertically with thickness drawn
454: * incrementally to either the right or left.
455: *
456: * @param g graphics object for drawing with
457: * @param x the x origin of the line
458: * @param y the y origin of the line
459: * @param thickness the thickness of the line
460: * @param horizontal or vertical (true for horizontal)
461: * @param right/bottom or left/top thickness (true for right or top),
462: * if true then the x or y origin will be incremented to provide
463: * thickness, if false, they'll be decremented. For vertical
464: * borders, x is incremented or decremented, for horizontal its y.
465: * Just set to true for north and west, and false for east and
466: * south.
467: * @returns length - returns the length of the line.
468: */
469: private int drawDashDotDot(Graphics g, int x, int y, int thickness,
470: boolean horizontal, boolean rightBottom) {
471:
472: for (int t = 0; t < thickness; t++) {
473: if (!rightBottom) {
474: t = 0 - t; //add negative thickness so we go the other way
475: //then we'll decrement instead of increment.
476: }
477: if (horizontal) {
478: g.drawLine(x, y + t, x + 5, y + t);
479: g.drawLine(x + 8, y + t, x + 10, y + t);
480: g.drawLine(x + 13, y + t, x + 15, y + t);
481: } else {
482: g.drawLine(x + t, y, x + t, y + 5);
483: g.drawLine(x + t, y + 8, x + t, y + 10);
484: g.drawLine(x + t, y + 13, x + t, y + 15);
485: }
486: }
487: return 18;
488: }
489:
490: /**
491: * @returns the line thickness for a border based on border type
492: */
493: private int getThickness(int thickness) {
494: int retval = 1;
495: switch (thickness) {
496: case HSSFCellStyle.BORDER_THIN:
497: retval = 2;
498: break;
499: case HSSFCellStyle.BORDER_MEDIUM:
500: retval = 3;
501: break;
502: case HSSFCellStyle.BORDER_THICK:
503: retval = 4;
504: break;
505: case HSSFCellStyle.BORDER_DASHED:
506: retval = 1;
507: break;
508: case HSSFCellStyle.BORDER_DASH_DOT_DOT:
509: retval = 1;
510: break;
511: case HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT:
512: retval = 3;
513: break;
514: case HSSFCellStyle.BORDER_HAIR:
515: retval = 1;
516: break;
517: default:
518: retval = 1;
519: }
520: return retval;
521: }
522:
523: }
|