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.core.exec.ui;
034:
035: import org.libresource.so6.core.conflict.editor.AbstractNode;
036: import org.libresource.so6.core.conflict.editor.DocumentNode;
037: import org.libresource.so6.core.conflict.editor.TextNode;
038: import org.libresource.so6.core.conflict.editor.ui.DefaultDocumentView;
039:
040: import java.awt.BorderLayout;
041: import java.awt.Color;
042: import java.awt.Component;
043: import java.awt.Point;
044: import java.awt.Rectangle;
045: import java.awt.Toolkit;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.awt.event.MouseAdapter;
049: import java.awt.event.MouseEvent;
050: import java.awt.event.WindowAdapter;
051: import java.awt.event.WindowEvent;
052:
053: import java.io.File;
054: import java.io.FileWriter;
055:
056: import javax.swing.BorderFactory;
057: import javax.swing.JFileChooser;
058: import javax.swing.JFrame;
059: import javax.swing.JLabel;
060: import javax.swing.JMenu;
061: import javax.swing.JMenuBar;
062: import javax.swing.JMenuItem;
063: import javax.swing.JOptionPane;
064: import javax.swing.JPanel;
065: import javax.swing.JPopupMenu;
066: import javax.swing.JScrollPane;
067: import javax.swing.JSplitPane;
068: import javax.swing.JTree;
069: import javax.swing.ToolTipManager;
070: import javax.swing.event.TreeSelectionEvent;
071: import javax.swing.event.TreeSelectionListener;
072: import javax.swing.tree.DefaultTreeModel;
073: import javax.swing.tree.TreeCellRenderer;
074: import javax.swing.tree.TreePath;
075:
076: /**
077: * @author smack
078: */
079: public class ConflictEditor extends JPanel implements ActionListener {
080: private String fileName;
081: private MyJTree conflictTree;
082: private JScrollPane scrollView;
083: private JPopupMenu popup;
084: private DefaultTreeModel conflictTreeModel;
085: private DocumentNode root;
086: private DefaultDocumentView docView;
087: Rectangle rect = new Rectangle(0, 10, 100, 100);
088:
089: public ConflictEditor(String fileName) throws Exception {
090: this .fileName = fileName;
091: root = new DocumentNode();
092: root.load(fileName);
093:
094: // Menu
095: JMenuBar menu = new JMenuBar();
096: JMenu file = new JMenu("File");
097: JMenuItem save = new JMenuItem("save current file as");
098: save.setActionCommand("SAVE");
099: save.addActionListener(this );
100:
101: JMenuItem exit = new JMenuItem("Quit");
102: exit.setActionCommand("EXIT");
103: exit.addActionListener(this );
104: file.add(save);
105: file.addSeparator();
106: file.add(exit);
107: menu.add(file);
108:
109: //
110: popup = new JPopupMenu();
111:
112: JMenuItem view = new JMenuItem("View");
113: view.setActionCommand("VIEW");
114: view.addActionListener(this );
115:
116: //popup.add(view);
117: JMenuItem edit = new JMenuItem("Edit");
118: edit.setActionCommand("EDIT");
119: edit.addActionListener(this );
120:
121: //popup.add(edit);
122: //popup.addSeparator();
123: JMenuItem delete = new JMenuItem("Hide / Show");
124: delete.setActionCommand("SHOW-HIDE");
125: delete.addActionListener(this );
126: popup.add(delete);
127:
128: //
129: conflictTreeModel = new DefaultTreeModel(root);
130: conflictTree = new MyJTree(conflictTreeModel);
131: conflictTree.setCellRenderer(new ConflictTreeRenderer());
132: conflictTree
133: .addTreeSelectionListener(new ConflictTreeListener());
134: conflictTree.addMouseListener(new PopupListener());
135:
136: //
137: docView = new DefaultDocumentView(root);
138: scrollView = new JScrollPane(docView);
139:
140: //
141: setLayout(new BorderLayout());
142: add(menu, BorderLayout.NORTH);
143: add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
144: new JScrollPane(conflictTree), scrollView),
145: BorderLayout.CENTER);
146:
147: //
148: ToolTipManager.sharedInstance().registerComponent(conflictTree);
149: ToolTipManager.sharedInstance().setInitialDelay(0);
150: }
151:
152: public static void main(String[] args) throws Exception {
153: JFrame f = new JFrame("Conflict editor : " + args[0]);
154: ConflictEditor ce = new ConflictEditor(args[0]);
155: f.getContentPane().add(ce);
156: f.addWindowListener(new WindowAdapter() {
157: public void windowClosing(WindowEvent e) {
158: System.exit(0);
159: }
160: });
161: f.setSize(400, 400);
162: f.setLocation(((int) Toolkit.getDefaultToolkit()
163: .getScreenSize().getWidth() - f.getWidth()) / 2,
164: ((int) Toolkit.getDefaultToolkit().getScreenSize()
165: .getHeight() - f.getHeight()) / 2);
166: f.setVisible(true);
167: }
168:
169: public void actionPerformed(ActionEvent e) {
170: String command = e.getActionCommand();
171: AbstractNode node = null;
172:
173: if (conflictTree.getSelectionPath() != null) {
174: node = ((AbstractNode) conflictTree.getSelectionPath()
175: .getLastPathComponent());
176: }
177:
178: //System.out.println(command);
179: if (command.equals("SHOW-HIDE")) {
180: node.setEnabled(!node.isEnabled());
181: docView.update(true);
182: docView.selectNode(node);
183: docView.update(false);
184:
185: int viewY = docView.getNodePosition(node);
186: scrollView.getViewport().setViewPosition(
187: new Point(0, viewY));
188: }
189:
190: if (command.equals("SAVE")) {
191: try {
192: JFileChooser chooser = new JFileChooser(new File(
193: fileName).getParentFile());
194:
195: if (chooser.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION) {
196: File output = chooser.getSelectedFile();
197: FileWriter fw = new FileWriter(output);
198: root.toDocument(fw, 0);
199: fw.close();
200: }
201: } catch (Exception e1) {
202: e1.printStackTrace();
203: JOptionPane.showMessageDialog(this , e1.getMessage());
204: }
205: }
206:
207: if (command.equals("EXIT")) {
208: System.exit(0);
209: }
210: }
211:
212: public class ConflictTreeRenderer extends JLabel implements
213: TreeCellRenderer {
214: public ConflictTreeRenderer() {
215: setOpaque(true);
216: setBorder(BorderFactory.createLineBorder(Color.white, 2));
217: }
218:
219: public Component getTreeCellRendererComponent(JTree tree,
220: Object value, boolean selected, boolean expanded,
221: boolean leaf, int row, boolean hasFocus) {
222: setBackground(selected ? Color.lightGray : Color.white);
223: setBorder(BorderFactory.createLineBorder(
224: selected ? Color.blue : Color.white, 2));
225:
226: //if (value instanceof HTNode) {
227: // setBackground(((HTNode) value).getColor());
228: //}
229: if (!((AbstractNode) value).isEnabled()) {
230: setBackground(Color.black);
231: }
232:
233: if (value instanceof TextNode) {
234: TextNode node = (TextNode) value;
235:
236: if (node.getParent() == null) {
237: setText("...");
238: } else {
239: setText(node.getHTMLText());
240: }
241: } else {
242: setText(value.toString());
243: }
244:
245: return this ;
246: }
247: }
248:
249: public class ConflictTreeListener implements TreeSelectionListener {
250: public void valueChanged(TreeSelectionEvent e) {
251: AbstractNode node = (AbstractNode) e.getPath()
252: .getLastPathComponent();
253: docView.selectNode(node);
254:
255: int viewY = docView.getNodePosition(node);
256: scrollView.getViewport().setViewPosition(
257: new Point(0, viewY));
258: }
259: }
260:
261: class PopupListener extends MouseAdapter {
262: public void mousePressed(MouseEvent e) {
263: maybeShowPopup(e);
264: }
265:
266: public void mouseReleased(MouseEvent e) {
267: maybeShowPopup(e);
268: }
269:
270: private void maybeShowPopup(MouseEvent e) {
271: if ((conflictTree.getSelectionPath() != null)
272: && e.isPopupTrigger()
273: && (conflictTree.getSelectionPath()
274: .getLastPathComponent() != null)) {
275: popup.show(e.getComponent(), e.getX(), e.getY());
276: }
277: }
278: }
279:
280: /*
281: public void showHyperTree() {
282: JFrame f = new JFrame("tree");
283: f.getContentPane().add(new HyperTree(root).getView());
284: f.setSize(400, 400);
285: f.setVisible(true);
286: }
287: */
288: public class MyJTree extends JTree {
289: public MyJTree(DefaultTreeModel model) {
290: super (model);
291: }
292:
293: public String getToolTipText(MouseEvent event) {
294: int x = event.getX();
295: int y = event.getY();
296: TreePath path = getPathForLocation(x, y);
297:
298: if ((path != null)
299: && path.getLastPathComponent() instanceof AbstractNode) {
300: return ((AbstractNode) path.getLastPathComponent())
301: .getOrigine();
302: }
303:
304: return null;
305: }
306: }
307: }
|