001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.forms.widgets;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.MouseEvent;
016: import org.eclipse.swt.graphics.*;
017: import org.eclipse.swt.widgets.Display;
018:
019: public class SelectionData {
020: public Display display;
021: public Color bg;
022: public Color fg;
023: private Point start;
024: private Point stop;
025: private ArrayList segments;
026: private boolean newLineNeeded;
027:
028: public SelectionData(MouseEvent e) {
029: display = e.display;
030: segments = new ArrayList();
031: start = new Point(e.x, e.y);
032: stop = new Point(e.x, e.y);
033: bg = e.display.getSystemColor(SWT.COLOR_LIST_SELECTION);
034: fg = e.display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
035: }
036:
037: public void markNewLine() {
038: newLineNeeded = true;
039: }
040:
041: public void addSegment(String text) {
042: if (newLineNeeded) {
043: segments.add(System.getProperty("line.separator")); //$NON-NLS-1$
044: newLineNeeded = false;
045: }
046: segments.add(text);
047: }
048:
049: public void update(MouseEvent e) {
050: //Control c = (Control)e.widget;
051: stop.x = e.x;
052: stop.y = e.y;
053: }
054:
055: public void reset() {
056: segments.clear();
057: }
058:
059: public String getSelectionText() {
060: StringBuffer buf = new StringBuffer();
061: for (int i = 0; i < segments.size(); i++) {
062: buf.append((String) segments.get(i));
063: }
064: return buf.toString();
065: }
066:
067: public boolean canCopy() {
068: return segments.size() > 0;
069: }
070:
071: private int getTopOffset() {
072: return start.y < stop.y ? start.y : stop.y;
073: }
074:
075: private int getBottomOffset() {
076: return start.y > stop.y ? start.y : stop.y;
077: }
078:
079: public int getLeftOffset(Locator locator) {
080: return isInverted(locator) ? stop.x : start.x;
081: }
082:
083: public int getLeftOffset(int rowHeight) {
084: return isInverted(rowHeight) ? stop.x : start.x;
085: }
086:
087: public int getRightOffset(Locator locator) {
088: return isInverted(locator) ? start.x : stop.x;
089: }
090:
091: public int getRightOffset(int rowHeight) {
092: return isInverted(rowHeight) ? start.x : stop.x;
093: }
094:
095: private boolean isInverted(Locator locator) {
096: int rowHeight = ((int[]) locator.heights
097: .get(locator.rowCounter))[0];
098: return isInverted(rowHeight);
099: }
100:
101: private boolean isInverted(int rowHeight) {
102: int deltaY = start.y - stop.y;
103: if (Math.abs(deltaY) > rowHeight) {
104: // inter-row selection
105: return deltaY > 0;
106: }
107: // intra-row selection
108: return start.x > stop.x;
109: }
110:
111: public boolean isEnclosed() {
112: return !start.equals(stop);
113: }
114:
115: public boolean isSelectedRow(Locator locator) {
116: if (!isEnclosed())
117: return false;
118: int rowHeight = ((int[]) locator.heights
119: .get(locator.rowCounter))[0];
120: return isSelectedRow(locator.y, rowHeight);
121: }
122:
123: public boolean isSelectedRow(int y, int rowHeight) {
124: if (!isEnclosed())
125: return false;
126: return (y + rowHeight >= getTopOffset() && y <= getBottomOffset());
127: }
128:
129: public boolean isFirstSelectionRow(Locator locator) {
130: if (!isEnclosed())
131: return false;
132: int rowHeight = ((int[]) locator.heights
133: .get(locator.rowCounter))[0];
134: return (locator.y + rowHeight >= getTopOffset() && locator.y <= getTopOffset());
135: }
136:
137: public boolean isFirstSelectionRow(int y, int rowHeight) {
138: if (!isEnclosed())
139: return false;
140: return (y + rowHeight >= getTopOffset() && y <= getTopOffset());
141: }
142:
143: public boolean isLastSelectionRow(Locator locator) {
144: if (!isEnclosed())
145: return false;
146: int rowHeight = ((int[]) locator.heights
147: .get(locator.rowCounter))[0];
148: return (locator.y + rowHeight >= getBottomOffset() && locator.y <= getBottomOffset());
149: }
150:
151: public boolean isLastSelectionRow(int y, int rowHeight) {
152: if (!isEnclosed())
153: return false;
154: return (y + rowHeight >= getBottomOffset() && y <= getBottomOffset());
155: }
156: }
|