001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.collab.channel.filesharing.filehandler;
042:
043: import org.openide.text.*;
044:
045: import java.awt.Color;
046:
047: import java.io.*;
048:
049: import java.util.*;
050:
051: import javax.swing.*;
052: import javax.swing.text.*;
053:
054: /**
055: * Bean that holds editorPane of a document, as well as its state
056: *
057: * @author Ayub Khan, ayub.khan@sun.com
058: * @version 1.0
059: */
060: public class EditorState extends Object {
061: ////////////////////////////////////////////////////////////////////////////
062: // Instance variables
063: ////////////////////////////////////////////////////////////////////////////
064: private JEditorPane editorPane = null;
065: private int refSave = 0;
066: private Position reference = null;
067: private int caretPosition = 0;
068: private Color selectedTextColor = null;
069: private int selectionStart = 0;
070: private int selectionEnd = 0;
071: private Color selectionColor = null;
072: private Caret caret = null;
073:
074: /**
075: * constructor
076: *
077: * @param editorPane
078: */
079: public EditorState(JEditorPane editorPane) {
080: super ();
081: this .editorPane = editorPane;
082: }
083:
084: ////////////////////////////////////////////////////////////////////////////
085: // methods
086: ////////////////////////////////////////////////////////////////////////////
087:
088: /**
089: *
090: * @return editorPane
091: */
092: public JEditorPane getEditorPane() {
093: return this .editorPane;
094: }
095:
096: /**
097: *
098: * @return editorPane
099: */
100: public void setEditorPane(JEditorPane editorPane) {
101: this .editorPane = editorPane;
102: }
103:
104: /**
105: *
106: */
107: public void save() {
108: caret = editorPane.getCaret();
109: caretPosition = caret.getDot(); //editorPane.getCaretPosition();
110:
111: selectedTextColor = editorPane.getSelectedTextColor();
112: selectionStart = editorPane.getSelectionStart();
113: selectionEnd = editorPane.getSelectionEnd();
114:
115: selectionColor = editorPane.getSelectionColor();
116:
117: refSave = caretPosition;
118:
119: if (selectionStart < refSave) {
120: refSave = selectionStart;
121: }
122:
123: if (selectionEnd < refSave) {
124: refSave = selectionEnd;
125: }
126:
127: try {
128: reference = NbDocument.createPosition(editorPane
129: .getDocument(), refSave, Position.Bias.Forward);
130: } catch (javax.swing.text.BadLocationException ex) {
131: reference = null;
132: }
133: }
134:
135: /**
136: *
137: */
138: public void resume() {
139: int adjust = 0;
140:
141: if (reference != null) {
142: adjust = reference.getOffset() - refSave;
143: }
144:
145: try {
146: caret.setDot(caretPosition + adjust);
147:
148: //editorPane.setCaretPosition(caretPosition+adjust);
149: editorPane.setSelectedTextColor(selectedTextColor);
150: editorPane.setSelectionStart(selectionStart + adjust);
151: editorPane.setSelectionEnd(selectionEnd + adjust);
152:
153: editorPane.setSelectionColor(selectionColor);
154: } catch (java.lang.IllegalArgumentException iargs) {
155: //ignore
156: }
157:
158: //release position
159: reference = null;
160: }
161: }
|