001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.application.studio.history.viewer.ui;
034:
035: import org.libresource.so6.application.studio.history.viewer.util.CommandFinder;
036: import org.libresource.so6.application.studio.history.viewer.util.FileHistoryPlayer;
037: import org.libresource.so6.application.studio.history.viewer.util.WorkspaceColorManager;
038: import org.libresource.so6.core.command.Command;
039: import org.libresource.so6.core.command.fs.AddFile;
040: import org.libresource.so6.core.command.fs.Remove;
041: import org.libresource.so6.core.command.text.AddBlock;
042: import org.libresource.so6.core.command.text.AddTxtFile;
043: import org.libresource.so6.core.command.text.DelBlock;
044:
045: import java.awt.BorderLayout;
046: import java.awt.Color;
047: import java.awt.Dimension;
048:
049: import java.io.FileReader;
050: import java.io.LineNumberReader;
051:
052: import java.util.Iterator;
053:
054: import javax.swing.JPanel;
055: import javax.swing.JScrollPane;
056: import javax.swing.JTextPane;
057: import javax.swing.text.BadLocationException;
058: import javax.swing.text.DefaultStyledDocument;
059: import javax.swing.text.MutableAttributeSet;
060: import javax.swing.text.SimpleAttributeSet;
061: import javax.swing.text.StyleConstants;
062: import javax.swing.text.StyleContext;
063: import javax.swing.text.StyledDocument;
064:
065: /**
066: * @author smack
067: */
068: public class FileHistoryPlayerImpl extends JPanel implements
069: FileHistoryPlayer {
070: private JTextPane textPane = null;
071: private StyledDocument doc = null;
072: private StyledDocument lineDoc = null;
073: private JTextPane lines = null;
074: private WorkspaceColorManager colorManager;
075: private int totalLines = 0;
076: private CommandFinder cmdFinder;
077: private int lineNb;
078: private int size;
079: private int stateViewed = 0;
080:
081: public FileHistoryPlayerImpl(CommandFinder cmdFinder) {
082: this .cmdFinder = cmdFinder;
083:
084: JPanel internal = new JPanel(new BorderLayout());
085: StyleContext context = new StyleContext();
086: doc = new DefaultStyledDocument();
087: textPane = new JTextPane(doc);
088: textPane.setEditable(false);
089: lineDoc = new DefaultStyledDocument();
090: lines = new JTextPane(lineDoc);
091: lines.setOpaque(true);
092: lines.setEditable(false);
093: lines.setBackground(Color.lightGray);
094: internal.add(lines, BorderLayout.WEST);
095: internal.add(textPane, BorderLayout.CENTER);
096: this .setLayout(new BorderLayout());
097: this .add(new JScrollPane(internal), BorderLayout.CENTER);
098: }
099:
100: public void setCommandFinder(CommandFinder cmdFinder) {
101: this .cmdFinder = cmdFinder;
102: }
103:
104: /**
105: * Player interface
106: */
107: public Command viewHistoryState(int index) {
108: try {
109: return processHistory(index);
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113:
114: return null;
115: }
116:
117: public String getState() throws Exception {
118: return doc.getText(0, doc.getLength());
119: }
120:
121: public void showLine(int lineNb, int size) {
122: this .lineNb = lineNb;
123: this .size = size;
124:
125: int pos = lineNb + 1;
126: MutableAttributeSet attr = new SimpleAttributeSet();
127: StyleConstants.setBackground(attr, Color.cyan);
128:
129: //
130: textPane.setPreferredSize(new Dimension(5000, 5000));
131:
132: StringBuffer buffer1 = new StringBuffer();
133: StringBuffer buffer2 = new StringBuffer();
134: StringBuffer buffer3 = new StringBuffer();
135:
136: for (int i = 1; i <= totalLines; i++) {
137: if (i < pos) {
138: buffer1.append(i + "\n");
139: } else if (i < (pos + size)) {
140: buffer2.append(i + "\n");
141: } else if (i > (pos + size)) {
142: buffer3.append(i + "\n");
143: }
144: }
145:
146: try {
147: lineDoc.remove(0, lineDoc.getLength());
148: lineDoc.insertString(lineDoc.getLength(), buffer1
149: .toString(), new SimpleAttributeSet());
150:
151: int caretPos = lineDoc.getLength();
152: lineDoc.insertString(lineDoc.getLength(), buffer2
153: .toString(), attr);
154: lineDoc.insertString(lineDoc.getLength(), buffer3
155: .toString(), new SimpleAttributeSet());
156: lines.setCaretPosition(caretPos);
157: } catch (BadLocationException e) {
158: e.printStackTrace();
159: }
160: }
161:
162: public void setWorkspaceColorManager(
163: WorkspaceColorManager colorManager) {
164: this .colorManager = colorManager;
165: }
166:
167: /**
168: * Business methodes
169: */
170: private Command processHistory(int lastIndex) throws Exception {
171: if (stateViewed < lastIndex) {
172: MutableAttributeSet attr = new SimpleAttributeSet();
173: StyleConstants.setBold(attr, false);
174: doc.setCharacterAttributes(0, doc.getLength(), attr, false);
175:
176: for (int i = stateViewed + 1; i <= lastIndex; i++) {
177: applyCommand((Command) cmdFinder.getOp(i),
178: i == lastIndex);
179: }
180: } else {
181: totalLines = 0;
182: doc.remove(0, doc.getLength());
183:
184: for (int i = 0; i <= lastIndex; i++) {
185: applyCommand((Command) cmdFinder.getOp(i),
186: i == lastIndex);
187: }
188: }
189:
190: stateViewed = lastIndex;
191:
192: return (Command) cmdFinder.getOp(lastIndex);
193: }
194:
195: private void applyCommand(Command cmd, boolean lastCommand)
196: throws Exception {
197: // Style
198: MutableAttributeSet attr = new SimpleAttributeSet();
199:
200: if (colorManager != null) {
201: StyleConstants.setBackground(attr, colorManager
202: .getColorOfWorkspace(cmd.getWsName()));
203: }
204:
205: StyleConstants.setBold(attr, cmd.isConflict() || lastCommand);
206:
207: if (cmd instanceof Remove) {
208: doc.remove(0, doc.getLength());
209: totalLines = 0;
210:
211: if (lastCommand) {
212: showLine(0, 0);
213: }
214: } else if (cmd instanceof AddTxtFile) {
215: doc.remove(0, doc.getLength());
216:
217: AddTxtFile atf = (AddTxtFile) cmd;
218: FileReader reader = new FileReader(atf.getAttachement());
219: LineNumberReader lnr = new LineNumberReader(reader);
220: String line;
221: totalLines = 0;
222:
223: while ((line = lnr.readLine()) != null) {
224: totalLines++;
225: doc.insertString(doc.getLength(), line + "\n", attr);
226: }
227:
228: if (lastCommand) {
229: showLine(0, totalLines);
230: }
231: } else if (cmd instanceof AddBlock) {
232: AddBlock ab = (AddBlock) cmd;
233: totalLines += ab.getSize();
234:
235: int startLine = ab.getInsertPoint() - 1;
236: int posInChar = -1;
237: String str = doc.getText(0, doc.getLength());
238:
239: for (int i = 0; i < startLine; i++) {
240: posInChar = str.indexOf("\n", posInChar + 1);
241: }
242:
243: String content = "";
244:
245: for (Iterator i = ab.getContent().iterator(); i.hasNext();) {
246: content += i.next();
247: }
248:
249: if (posInChar != -1) {
250: doc.insertString(posInChar + 1, content, attr);
251: textPane.setCaretPosition(posInChar + 1);
252: } else {
253: doc.insertString(0, content, attr);
254: textPane.setCaretPosition(0);
255: }
256:
257: if (lastCommand) {
258: showLine(startLine, ab.getSize());
259: }
260: } else if (cmd instanceof DelBlock) {
261: DelBlock db = (DelBlock) cmd;
262: totalLines -= db.getSize();
263:
264: int delPoint = db.getDeletePoint() - 1;
265: int nbLine = db.getSize();
266: int startPos = -1;
267: String str = doc.getText(0, doc.getLength());
268:
269: for (int i = 0; i < delPoint; i++) {
270: startPos = str.indexOf("\n", startPos + 1);
271: }
272:
273: String content = "";
274:
275: for (Iterator i = db.getOldContent().iterator(); i
276: .hasNext();) {
277: content += i.next();
278: }
279:
280: if (startPos == -1) {
281: doc.remove(0, content.length());
282: textPane.setCaretPosition(0);
283: } else {
284: doc.remove(startPos, content.length());
285: textPane.setCaretPosition(startPos);
286: }
287:
288: if (lastCommand) {
289: showLine(delPoint, db.getSize());
290: }
291: } else if (cmd instanceof AddFile) {
292: if (lastCommand) {
293: showLine(0, 0);
294: }
295: }
296: }
297:
298: public void clearText() {
299: try {
300: doc.remove(0, doc.getLength());
301: } catch (BadLocationException e) {
302: e.printStackTrace();
303: }
304:
305: totalLines = 0;
306: showLine(0, 0);
307: }
308:
309: public int getLineToShow() {
310: return lineNb;
311: }
312:
313: public int getNbLineToShow() {
314: return size;
315: }
316: }
|