Source code: org/bdgp/apps/dagedit/plugin/HistoryPlugin.java
1 package org.bdgp.apps.dagedit.plugin;
2
3 import org.bdgp.util.*;
4 import org.bdgp.swing.*;
5 import org.bdgp.apps.dagedit.gui.*;
6 import org.bdgp.apps.dagedit.gui.event.*;
7 import org.bdgp.apps.dagedit.dataadapter.*;
8 import org.bdgp.apps.dagedit.datamodel.*;
9 import java.util.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.io.*;
13 import javax.swing.*;
14 import javax.swing.tree.*;
15 import javax.swing.event.*;
16
17 public class HistoryPlugin extends DEPlugin {
18
19 public final static int TAB_SIZE = 3;
20
21 Vector histories;
22 JTree sessionList;
23 HistoryTreeModel model;
24 DETermReloadListener reloadListener;
25 RootChangeListener rootListener;
26 TreeSelectionListener selectListener;
27 JButton saveButton = new JButton("Save History");
28
29 Component lastGUI = null;
30
31 public HistoryPlugin() {
32 sessionList = new JTree();
33 sessionList.setFont(new Font("Dialog",0,10));
34 sessionList.putClientProperty("JTree.lineStyle", "Angled");
35 sessionList.getSelectionModel().
36 setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
37 DefaultTreeCellRenderer rend = new DefaultTreeCellRenderer();
38 rend.setLeafIcon(null);
39 rend.setClosedIcon(null);
40 rend.setOpenIcon(null);
41 sessionList.setCellRenderer(rend);
42 }
43
44 public String getName() {
45 return "History plugin";
46 }
47
48 public void init(MultiProperties props) {
49 histories = new Vector();
50 saveButton.setFont(controller.getDefaultFont());
51 saveButton.setBackground(Preferences.defaultButtonColor());
52 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
53 setOpaque(false);
54 layoutPlugin();
55 attachListeners();
56 }
57
58 protected void saveHistory() {
59 JFileChooser chooser = new JFileChooser();
60 if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
61 File file = chooser.getSelectedFile();
62 if (file.exists() &&
63 JOptionPane.showConfirmDialog(this,
64 "Overwrite file "+
65 file,
66 "Overwrite file?",
67 JOptionPane.YES_NO_OPTION) !=
68 JOptionPane.YES_OPTION)
69 return;
70 DEEditHistory history = (DEEditHistory) sessionList.
71 getSelectionPath().getLastPathComponent();
72 try {
73 PrintWriter writer = new PrintWriter(new FileWriter(file));
74 writer.println("!date : "+history.getDate());
75 writer.println("!user : "+history.getUser());
76 if (history.getComment() != null)
77 writer.println("!comment: "+history.getComment());
78 printHistory(writer, 0, history);
79 writer.close();
80 } catch (IOException e) {
81 JOptionPane.showMessageDialog(this, "Could not save file "+
82 file+"because of error: "+
83 e.getMessage());
84 }
85 }
86 }
87
88 protected void printHistory(PrintWriter writer, int indent, Object obj) {
89 String str = StringUtil.repeat(" ", indent);
90 writer.println(str+"* "+obj.toString());
91 int childCount = model.getChildCount(obj);
92 for(int i=0; i < childCount; i++) {
93 Object child = model.getChild(obj, i);
94 printHistory(writer, indent+TAB_SIZE, child);
95 }
96 }
97
98 private void attachListeners() {
99
100 rootListener = new RootChangeListener() {
101 public void changeRoot(RootChangeEvent e) {
102 layoutPlugin();
103 sessionList.expandPath(model.getActiveHistoryPath());
104 }
105 };
106
107 reloadListener = new DETermReloadListener() {
108 public void reload(DETermReloadEvent e) {
109 model.reloadActiveHistory();
110 sessionList.expandPath(model.getActiveHistoryPath());
111 resize();
112 }
113 };
114
115 selectListener = new TreeSelectionListener() {
116 public void valueChanged(TreeSelectionEvent e) {
117 updateGUI();
118 }
119 };
120
121 sessionList.addTreeSelectionListener(selectListener);
122 controller.addListener(rootListener);
123 controller.addListener(reloadListener);
124 saveButton.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 saveHistory();
127 }
128 });
129 }
130
131 protected void updateGUI() {
132 if (lastGUI != null)
133 remove(lastGUI);
134 if (sessionList.getSelectionPath() != null) {
135 Object selected = sessionList.getSelectionPath().
136 getLastPathComponent();
137 lastGUI = getComponentForItem(selected);
138 add(lastGUI, "South");
139 } else
140 lastGUI = null;
141 resize();
142 repaint();
143 }
144
145 protected Component getComponentForItem(Object object) {
146 if (object instanceof DEEditHistory) {
147 JPanel panel = new JPanel();
148 panel.setBackground(Color.white);
149 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
150 JLabel label = new JLabel();
151 JLabel commentLabel = new JLabel("Comment");
152 commentLabel.setFont(controller.getDefaultFont());
153 JTextArea descTextArea = new JTextArea(5, 30);
154 descTextArea.setEditable(false);
155 descTextArea.setWrapStyleWord(true);
156 descTextArea.setLineWrap(true);
157 descTextArea.setFont(controller.getDefaultFont());
158 JScrollPane descPane =
159 new JScrollPane(descTextArea,
160 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
161 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
162
163 label.setText(((DEEditHistory) object).getTitle());
164 descTextArea.setText(((DEEditHistory) object).getComment());
165 label.setAlignmentX(LEFT_ALIGNMENT);
166 commentLabel.setAlignmentX(LEFT_ALIGNMENT);
167 descPane.setAlignmentX(LEFT_ALIGNMENT);
168
169 JPanel buttonBox = new JPanel();
170 buttonBox.setLayout(new BoxLayout(buttonBox, BoxLayout.X_AXIS));
171 buttonBox.add(Box.createHorizontalGlue());
172 buttonBox.add(saveButton);
173 buttonBox.add(Box.createHorizontalGlue());
174 buttonBox.setAlignmentX(LEFT_ALIGNMENT);
175 buttonBox.setOpaque(false);
176
177 panel.add(label);
178 panel.add(Box.createVerticalStrut(10));
179 panel.add(commentLabel);
180 panel.add(descPane);
181 panel.add(buttonBox);
182 return panel;
183 } else {
184 JTextArea descTextArea = new JTextArea(5, 30);
185 descTextArea.setEditable(false);
186 descTextArea.setWrapStyleWord(true);
187 descTextArea.setLineWrap(true);
188 descTextArea.setFont(controller.getDefaultFont());
189 JScrollPane descPane =
190 new JScrollPane(descTextArea,
191 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
192 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
193 descTextArea.setText(object.toString());
194 return descPane;
195 }
196 }
197
198 public void cleanup() {
199 controller.removeListener(rootListener);
200 controller.removeListener(reloadListener);
201 }
202
203 protected void layoutPlugin() {
204 removeAll();
205 setLayout(new BorderLayout());
206 try {
207 DEDataAdapterI adapter = controller.getLastAdapter();
208 histories = adapter.getHistories();
209 } catch (Exception e) {
210 JLabel label = new JLabel("Note: ancient history not supported");
211 add(label);
212 }
213 model = new HistoryTreeModel(histories, controller);
214 sessionList.setModel(model);
215 JScrollPane pane =
216 new JScrollPane(sessionList,
217 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
218 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
219 SwingUtil.setPreferredWidth(pane, 200);
220 add(pane, "Center");
221 validate();
222 }
223 }
224