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.command.Command;
036: import org.libresource.so6.core.command.text.AddBlock;
037: import org.libresource.so6.core.command.text.AddTxtFile;
038: import org.libresource.so6.core.command.text.DelBlock;
039: import org.libresource.so6.core.command.xml.AddXmlFile;
040: import org.libresource.so6.core.engine.OpVectorFsImpl;
041: import org.libresource.so6.core.engine.PatchFile;
042: import org.libresource.so6.core.engine.util.FileUtils;
043: import org.libresource.so6.core.engine.util.InfoPatchHandler;
044: import org.libresource.so6.core.ui.util.StyledUI;
045:
046: import java.awt.BorderLayout;
047: import java.awt.Color;
048: import java.awt.Component;
049: import java.awt.event.WindowAdapter;
050: import java.awt.event.WindowEvent;
051:
052: import java.io.File;
053: import java.io.FileReader;
054:
055: import java.net.URL;
056:
057: import java.util.Iterator;
058:
059: import javax.swing.AbstractListModel;
060: import javax.swing.JFrame;
061: import javax.swing.JLabel;
062: import javax.swing.JList;
063: import javax.swing.JPanel;
064: import javax.swing.JScrollPane;
065: import javax.swing.JSplitPane;
066: import javax.swing.JTextArea;
067: import javax.swing.ListCellRenderer;
068: import javax.swing.event.ListSelectionEvent;
069: import javax.swing.event.ListSelectionListener;
070:
071: import javax.xml.parsers.SAXParser;
072: import javax.xml.parsers.SAXParserFactory;
073:
074: /**
075: * @author smack
076: */
077: public class PatchInspector extends JPanel implements
078: ListSelectionListener, StyledUI {
079: private File workingDir;
080: private File cmds;
081: private File attach;
082: private OpVectorFsImpl opVector;
083:
084: // UI
085: private CmdAdapter adapter;
086: private JList cmdsList;
087: private JTextArea content;
088: private JSplitPane split;
089: private JLabel infos;
090:
091: public PatchInspector() throws Exception {
092: // init
093: workingDir = FileUtils.createTmpDir();
094: cmds = new File(workingDir, "cmds");
095: cmds.mkdirs();
096: attach = new File(workingDir, "attach");
097: attach.mkdirs();
098: opVector = new OpVectorFsImpl(cmds.getPath());
099:
100: // ui
101: infos = new JLabel();
102: adapter = new CmdAdapter();
103: cmdsList = new JList(adapter);
104: cmdsList.addListSelectionListener(this );
105: cmdsList.setCellRenderer(new Renderer());
106: content = new JTextArea();
107: content.setEditable(false);
108: split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
109: new JScrollPane(cmdsList), new JScrollPane(content));
110: setLayout(new BorderLayout());
111: add(infos, BorderLayout.NORTH);
112: add(split, BorderLayout.CENTER);
113: }
114:
115: public void setSplitLocation(int location) {
116: split.setDividerLocation(location);
117: }
118:
119: public void setSplitLocation(double pourcent) {
120: split.setDividerLocation(pourcent);
121: }
122:
123: public void load(URL url) throws Exception {
124: PatchFile pf = new PatchFile(url.getFile());
125: pf.buildOpVector(url.openStream(), opVector, attach.getPath(),
126: null);
127: adapter.update();
128: retreivePatchInfo(url.toString());
129: }
130:
131: public void load(String fileName) throws Exception {
132: PatchFile pf = new PatchFile(fileName);
133: pf.buildOpVector(opVector, attach.getPath(), null);
134: adapter.update();
135: retreivePatchInfo(fileName);
136: }
137:
138: private void retreivePatchInfo(String uri) throws Exception {
139: SAXParserFactory factory = SAXParserFactory.newInstance();
140: SAXParser saxParser = factory.newSAXParser();
141: InfoPatchHandler infoPatch = new InfoPatchHandler();
142: saxParser.parse(new File(uri), infoPatch);
143:
144: StringBuffer output = new StringBuffer();
145:
146: //"" + infoPatch.getComment() + "/" + infoPatch.getFromTicket()+ "/" +
147: // infoPatch.getToTicket()+ "/" + infoPatch.getWsName()
148: output.append("<html><table>");
149: output.append("<tr><td>From ticket</td><td><td>:</td>");
150: output.append(Long.toString(infoPatch.getFromTicket()));
151: output.append("</td></tr>");
152: output.append("<tr><td>To ticket</td><td><td>:</td>");
153: output.append(Long.toString(infoPatch.getToTicket()));
154: output.append("</td></tr>");
155: output.append("<tr><td>Origine</td><td><td>:</td>");
156: output.append(infoPatch.getWsName());
157: output.append("</td></tr>");
158: output.append("<tr><td>Comment</td><td><td>:</td>");
159: output.append(infoPatch.getComment());
160: output.append("</td></tr>");
161: output.append("</table></html>");
162: infos.setText(output.toString());
163: }
164:
165: public static void main(String[] args) throws Exception {
166: File file = new File(args[0]);
167: JFrame f = new JFrame("Patch inspector: " + args[0]);
168: PatchInspector pi = new PatchInspector();
169:
170: if (file.exists()) {
171: pi.load(file.getPath());
172: } else {
173: pi.load(new URL(args[0]));
174: }
175:
176: f.getContentPane().setLayout(new BorderLayout());
177: f.getContentPane().add(pi, BorderLayout.CENTER);
178: f.setSize(400, 400);
179: pi.setSplitLocation(200);
180: f.addWindowListener(new WindowAdapter() {
181: public void windowClosing(WindowEvent e) {
182: System.exit(0);
183: }
184: });
185: f.setVisible(true);
186: }
187:
188: public void valueChanged(ListSelectionEvent e) {
189: Command cmd = (Command) cmdsList.getSelectedValue();
190: content.setText("");
191:
192: if (cmd instanceof AddTxtFile || cmd instanceof AddXmlFile) {
193: try {
194: FileReader fr = new FileReader(cmd.getAttachement());
195: char[] buffer = new char[1024];
196: int length = -1;
197:
198: while ((length = fr.read(buffer)) != -1) {
199: content.append(new String(buffer, 0, length));
200: }
201: } catch (Exception e1) {
202: content.setText("Error while trying to load command "
203: + cmd);
204: }
205: } else if (cmd instanceof AddBlock) {
206: AddBlock ab = (AddBlock) cmd;
207:
208: for (Iterator i = ab.getContent().iterator(); i.hasNext();) {
209: content.append((String) i.next());
210: }
211: } else if (cmd instanceof DelBlock) {
212: DelBlock ab = (DelBlock) cmd;
213:
214: for (Iterator i = ab.getOldContent().iterator(); i
215: .hasNext();) {
216: content.append((String) i.next());
217: }
218: } else {
219: content.setText("No preview available");
220: }
221: }
222:
223: public void setStyle(Color back, Color forground) {
224: }
225:
226: public class CmdAdapter extends AbstractListModel {
227: public void update() {
228: fireContentsChanged(this , 0, getSize());
229: }
230:
231: public int getSize() {
232: try {
233: return opVector.size();
234: } catch (Exception e) {
235: return 0;
236: }
237: }
238:
239: public Object getElementAt(int index) {
240: return opVector.getCommand(index);
241: }
242: }
243:
244: private class Renderer extends JLabel implements ListCellRenderer {
245: public Renderer() {
246: setOpaque(true);
247: }
248:
249: public Component getListCellRendererComponent(JList list,
250: Object value, int index, boolean isSelected,
251: boolean cellHasFocus) {
252: Command cmd = (Command) value;
253: setText(cmd.getTicket() + " : " + cmd.toString());
254: setBackground(isSelected ? Color.decode("#aaaaee")
255: : Color.white);
256:
257: return this;
258: }
259: }
260: }
|