View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.BorderLayout;
24  import java.awt.CardLayout;
25  import java.awt.Color;
26  import java.awt.Component;
27  import java.awt.Container;
28  import java.awt.Cursor;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.GridLayout;
32  import java.awt.event.WindowAdapter;
33  import java.awt.event.WindowEvent;
34  import java.io.ByteArrayInputStream;
35  import java.io.File;
36  import java.io.IOException;
37  import java.io.UnsupportedEncodingException;
38  import java.util.ArrayList;
39  
40  import javax.swing.JButton;
41  import javax.swing.JFrame;
42  import javax.swing.JLabel;
43  import javax.swing.JOptionPane;
44  import javax.swing.JPanel;
45  import javax.swing.JScrollPane;
46  import javax.swing.JTextField;
47  import javax.swing.SwingUtilities;
48  import javax.swing.filechooser.FileFilter;
49  
50  import no.sintef.file.FileSuffixFilter;
51  import no.sintef.file.IOUtils;
52  import no.sintef.file.MimeTypeEnum;
53  import no.sintef.xml.XmlHelper;
54  
55  import org.jdesktop.jdic.filetypes.Action;
56  import org.jdesktop.jdic.filetypes.Association;
57  import org.jdesktop.jdic.filetypes.AssociationService;
58  import org.sintef.umt.hutntree.HutnTreeView;
59  import org.w3c.dom.Document;
60  import org.w3c.dom.Node;
61  import org.xml.sax.InputSource;
62  
63  import com.vikash.firsttool.UI.MainFrame;
64  
65  import coras.client.SwingWorker;
66  import coras.common.CorasElement;
67  import coras.common.CorasRepresentation;
68  import coras.representations.BinaryRepresentation;
69  import coras.representations.BitmapImage;
70  import coras.representations.DgmModel;
71  import coras.representations.DiagramRepresentation;
72  import coras.representations.DocumentRepresentation;
73  import coras.representations.RepresentationFactory;
74  import coras.representations.RepresentationTypeEnum;
75  import coras.representations.SVGImage;
76  import coras.representations.UMLModelRepresentation;
77  import coras.representations.XmiLightRepresentation;
78  import coras.representations.XmlRepresentation;
79  import coras.types.IUMLModel;
80  /***
81   * @author fvr
82   *
83   * TODO To change the template for this generated type comment go to
84   * Window - Preferences - Java - Code Style - Code Templates
85   */
86  public class RepresentationPanel extends JPanel {
87  
88      public static final String DIRTY = "dirty";
89  
90      private CorasRepresentation representation = null;
91  	private RepresentationTypeEnum type = null;
92  	private Node xmiLight = null;
93  	private boolean dirty = false;
94  	private boolean editable = false;
95  
96  	private JTextField fileField = null;
97  	private JButton importButton = null;
98  	private JPanel contentPanel = null;
99  	private JPanel imagePanel = null;
100 	private JPanel textPanel = null;
101 	private JPanel loadingPanel = null;
102 	private JPanel emptyPanel = null;
103 	private CorasTextArea textArea = null;
104 	
105 	private JPanel controlPanel = null;  //  @jve:decl-index=0:visual-constraint="407,89"
106 	private JButton openButton = null;
107 	private JButton exportButton = null;
108 	private JLabel fileLabel = null;
109 	private JPanel buttonPanel = null;
110 	private JScrollPane jScrollPane = null;
111 	private JPanel htmlPanel = null;
112 	private JLabel htmlLabel = null;
113 
114 	private boolean openExternal;
115 
116 	/***
117 	 * This method initializes jPanel	
118 	 * 	
119 	 * @return javax.swing.JPanel	
120 	 */    
121 	private JPanel getHtmlPanel() {
122 		if (htmlPanel == null) {
123 			htmlLabel = new JLabel();
124 			htmlPanel = new JPanel();
125 			htmlPanel.setLayout(new BorderLayout());
126 			htmlPanel.setName("htmlPanel");
127 			htmlLabel.setText("JLabel");
128 			htmlPanel.setBackground(Color.WHITE);
129 			htmlPanel.add(htmlLabel, java.awt.BorderLayout.NORTH);
130 		}
131 		return htmlPanel;
132 	}
133 
134 	/***
135 	 * This is the default constructor
136 	 */
137 	public RepresentationPanel() {
138 		super();
139 		initialize();
140 	}
141 
142 	public RepresentationPanel(CorasElement element, CorasRepresentation representation, RepresentationTypeEnum type) {
143 	    this();
144 	    this.representation = representation;
145 	    this.type = type;
146 	    if (element instanceof IUMLModel && type == RepresentationTypeEnum.XMI_LIGHT) {
147 	    	XmiLightRepresentation xmiLightRepresentation = ((IUMLModel)element).getXmiLight();
148 	    	if (xmiLightRepresentation != null) {
149 				xmiLight = XmlHelper.getRootElement(xmiLightRepresentation.getXmlContent());
150 			} else {
151 				xmiLight = null;
152 			}
153 	    }
154 	    update();
155 	}
156 	
157 	public boolean isDirty() {
158 		return dirty;
159 	}
160 	
161 	private void setDirty(boolean newDirty) {
162 		boolean oldDirty = dirty;
163 		dirty = newDirty;
164 		if (oldDirty != newDirty) {
165 			firePropertyChange(DIRTY, oldDirty, newDirty);
166 		}
167 	}
168 	
169 	public void makeClean() {
170 		setDirty(false);
171 	}
172 	
173 	private void makeDirty() {
174 		setDirty(true);
175 	}
176 	
177 	public synchronized void resetData() {
178 	    representation = null;
179 	    // Let type remain the same
180 	    makeClean();
181 	    update();
182 	}
183 
184     public boolean isEditable() {
185     	return editable;
186     }
187 
188     /***
189      * @param editing
190      */
191     public void setEditable(boolean editable) {
192     	if (this.editable != editable) {
193     		this.editable = editable;
194     		updateButtons();
195     	}
196     }
197 	
198 	public CorasRepresentation getRepresentation() {
199 	    return representation;
200 	}
201 	
202 	public synchronized void setRepresentation(CorasRepresentation representation) {
203 	    if (this.representation == representation) {
204 	        return;
205 	    }
206 	    this.representation = representation;
207 //	    if (type != RepresentationTypeEnum.XMI_LIGHT)
208 //	    	type = representation != null ? representation.getType() : null;
209 	    makeDirty();
210 	    update();
211 	}
212 	
213 	public synchronized void setType(RepresentationTypeEnum type) {
214 	    if (this.type == type) {
215 	        return;
216 	    }
217 	    if (type == null) {
218 	        return;
219 	    } else {
220 	        representation = null;
221             this.type = type;
222         }
223 	    update();
224 	}
225 	
226     private void update() {
227     	final CorasRepresentation theRepresentation = getRepresentation();
228     	final String filename = theRepresentation != null ? theRepresentation.getFilename() : null;
229     	
230     	final CardLayout layout = (CardLayout) getContentPanel().getLayout();
231 
232     	setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
233     	Runnable runner = new Runnable() {
234 			public void run() {
235 				layout.show(getContentPanel(), getLoadingPanel().getName());
236 				getFileField().setText(filename != null ? filename : "");
237 				getTextArea().setText("");
238 				getImagePanel().removeAll();
239 				getImagePanel().revalidate();
240 				getImportButton().setEnabled(false);
241 				getExportButton().setEnabled(false);
242 				getOpenButton().setEnabled(false);
243 			}
244     	};
245 			
246     	try {
247     		if (SwingUtilities.isEventDispatchThread()) {
248     			runner.run();
249     		} else {
250     			SwingUtilities.invokeAndWait(runner);
251     		}
252 		} catch (Exception e) {
253 		}
254     	
255     	new SwingWorker() {
256 			public Object construct() {
257 				MimeTypeEnum mimeType = MimeTypeEnum.getMimeType(filename);
258 				if (theRepresentation instanceof DiagramRepresentation) {
259 					return ((DiagramRepresentation)theRepresentation).getUIComponent();
260 				} else if (type == RepresentationTypeEnum.XMI_LIGHT) {
261 					if (xmiLight == null) {
262 						return null;
263 					}
264 //					System.out.println("XMI light: " + new String(XmlHelper.xmlToUtf8(xmiLight, true)));
265 					HutnTreeView tree = new HutnTreeView();
266 					ArrayList a = new ArrayList();
267 					a.add(xmiLight);
268 					tree.addModelElements(a);
269 					return tree;
270 				} else if (theRepresentation instanceof XmlRepresentation) {
271 					Node n = ((XmlRepresentation)theRepresentation).getXmlContent();
272 					if (n == null) {
273 						return null;
274 					}
275 					byte[] bytes = XmlHelper.xmlToUtf8(n, true);
276 					try {
277 						return new String(bytes, "UTF-8");
278 					} catch (UnsupportedEncodingException e) {
279 						return new String(bytes);
280 					}
281 				} else if (theRepresentation instanceof DocumentRepresentation) {
282 					byte[] bytes = ((DocumentRepresentation)theRepresentation).getBinaryContent();
283 					try {
284 						Component result = null;
285 						if (mimeType == MimeTypeEnum.IMAGE_SVG || mimeType == MimeTypeEnum.TEXT_XML) {
286 							Document doc = XmlHelper.parse(new InputSource(new ByteArrayInputStream(bytes)));
287 							result = SVGImage.createUIComponent(doc);
288 						} 
289 						if (result == null && MimeTypeEnum.isImage(mimeType)) {
290 							result = BitmapImage.createUIComponent(bytes);
291 						}
292 						if (result != null) {
293 							return result;
294 						}
295 					} catch (Exception e) {
296 						e.printStackTrace();
297 					}
298 					if (MimeTypeEnum.isText(mimeType)) {
299 						String s = new String(bytes);
300 						System.out.println(s);
301 						if (mimeType == MimeTypeEnum.TEXT_HTML) {
302 							s = s.replaceAll("<[Mm][Ee][Tt][Aa].*>", "");
303 							int index = s.toLowerCase().indexOf("<html");
304 							if (index > 0)
305 								s = s.substring(index); 
306 						}
307 						return s;
308 					} else {
309 						return null;
310 					}
311 				} else {
312 					return null;
313 				}
314 			}
315 
316 			public void finished() {
317 				Object o = get();
318 				if (o instanceof Component) {
319 					Component comp = (Component) o;
320 					GridBagConstraints gbc = new GridBagConstraints();
321 					gbc.anchor = GridBagConstraints.NORTHEAST;
322 					gbc.fill = GridBagConstraints.BOTH;
323 					gbc.weightx = gbc.weighty = 1.0;
324 					gbc.gridx = gbc.gridy = 0;
325 					GridBagLayout gbl = (GridBagLayout) getImagePanel().getLayout();
326 					gbl.setConstraints(comp, gbc);
327 					getImagePanel().add(comp);
328 					layout.show(getContentPanel(), getImagePanel().getName());
329 				} else if (o instanceof String) {
330 					String text = (String) o;
331 					if (text.toLowerCase().indexOf("<html>") != -1) {
332 						htmlLabel.setText(text);
333 						layout.show(getContentPanel(), getHtmlPanel().getName());
334 					} else {
335 						getTextArea().setText(text);
336 						layout.show(getContentPanel(), getTextPanel().getName());
337 					}
338 				} else {
339 					layout.show(getContentPanel(), getEmptyPanel().getName());
340 				}
341 				updateButtons();
342 				setCursor(Cursor.getDefaultCursor());
343 			}
344     	}.start();
345 	}
346 	
347 	/***
348 	 * This method initializes this
349 	 * 
350 	 * @return void
351 	 */
352 	private  void initialize() {
353 		this.setLayout(new BorderLayout());
354 		this.setBounds(0, 0, 400, 300);
355 		this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
356 		this.add(getControlPanel(), java.awt.BorderLayout.SOUTH);
357 	}
358     /***
359      * This method initializes jButton1	
360      * 	
361      * @return javax.swing.JButton	
362      */    
363     private JButton getExportButton() {
364     	if (exportButton == null) {
365     		exportButton = new JButton();
366     		exportButton.setText("Export");
367     		exportButton.setToolTipText("Export to file");
368     		exportButton.setMnemonic(java.awt.event.KeyEvent.VK_S);
369     		exportButton.setEnabled(false);
370     		exportButton.addActionListener(new java.awt.event.ActionListener() { 
371     			public void actionPerformed(java.awt.event.ActionEvent e) {    
372     			    final CorasRepresentation representation = getRepresentation();
373 				    if (representation == null || representation.getType() == null) {
374 				        return; // FIXME error
375 				    }
376     			    final File f = IOUtils.showSaveDialog(representation.getFilename(), exportButton.getTopLevelAncestor());
377     			    if (f != null) {
378     			    	final Container owner = getTopLevelAncestor();
379     			    	owner.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
380     			        new SwingWorker() {
381                             public Object construct() {
382                                 byte[] bytes;
383                                 if (representation instanceof BinaryRepresentation) {
384                                     bytes = ((BinaryRepresentation)representation).getBinaryContent();
385                                 } else {
386                                     bytes = XmlHelper.xmlToUtf8(((XmlRepresentation)representation).getXmlContent(), true);
387                                 }
388                                 try {
389                                     IOUtils.saveFile(bytes, f);
390                                     return null;
391                                 } catch (IOException e) {
392                                     return e;
393                                 }
394                             }
395                             public void finished() {
396                             	owner.setCursor(Cursor.getDefaultCursor());
397                                 Object o = get();
398                                 if (o instanceof Exception) {
399                                     Exception e = (Exception) o;
400                                     JOptionPane.showMessageDialog(exportButton.getTopLevelAncestor(), "<P>An error occured while saving the fule.</P><P>Error message:" + e.getMessage() + "</P>", "Error saving file", JOptionPane.ERROR_MESSAGE);
401                                 }
402                             }
403     			        }.start();
404     			    }
405     			}
406     		});
407     	}
408     	return exportButton;
409     }
410     /***
411      * This method initializes jPanel	
412      * 	
413      * @return javax.swing.JPanel	
414      */    
415     private JPanel getButtonPanel() {
416     	if (buttonPanel == null) {
417     		buttonPanel = new JPanel();
418     		buttonPanel.add(getImportButton(), null);
419     		buttonPanel.add(getExportButton(), null);
420     		buttonPanel.add(getOpenButton(), null);
421     	}
422     	return buttonPanel;
423     }
424     /***
425      * This method initializes jScrollPane1	
426      * 	
427      * @return javax.swing.JScrollPane	
428      */    
429     private JScrollPane getJScrollPane() {
430     	if (jScrollPane == null) {
431     		jScrollPane = new JScrollPane();
432     		jScrollPane.setViewportView(getContentPanel());
433     	}
434     	return jScrollPane;
435     }
436     /***
437      * This method initializes jPanel	
438      * 	
439      * @return javax.swing.JPanel	
440      */    
441     private JPanel getControlPanel() {
442     	if (controlPanel == null) {
443     		fileLabel = new JLabel();
444     		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
445     		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
446     		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
447     		controlPanel = new JPanel();
448     		controlPanel.setLayout(new GridBagLayout());
449     		gridBagConstraints1.gridx = 1;
450     		gridBagConstraints1.gridy = 0;
451     		gridBagConstraints1.weightx = 1.0D;
452     		gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
453     		gridBagConstraints31.gridx = 0;
454     		gridBagConstraints31.gridy = 0;
455     		gridBagConstraints31.insets = new java.awt.Insets(0,0,0,5);
456     		fileLabel.setText("Filename:");
457     		gridBagConstraints4.gridx = 0;
458     		gridBagConstraints4.gridy = 1;
459     		gridBagConstraints4.gridwidth = 2;
460     		controlPanel.add(getFileField(), gridBagConstraints1);
461     		controlPanel.add(fileLabel, gridBagConstraints31);
462     		controlPanel.add(getButtonPanel(), gridBagConstraints4);
463     	}
464     	return controlPanel;
465     }
466     /***
467      * This method initializes jButton	
468      * 	
469      * @return javax.swing.JButton	
470      */    
471     private JButton getOpenButton() {
472     	if (openButton == null) {
473     		openButton = new JButton();
474     		openButton.setText("Open");
475     		openButton.setMnemonic(java.awt.event.KeyEvent.VK_O);
476     		openButton.setVisible(true);
477     		openButton.addActionListener(new java.awt.event.ActionListener() { 
478     			public void actionPerformed(java.awt.event.ActionEvent ev) {
479     				CorasRepresentation theRepresentation = getRepresentation();
480     				if (theRepresentation instanceof DgmModel
481     						|| (theRepresentation == null && type == RepresentationTypeEnum.XMI_LIGHT)) {
482 						openInCorasModellingTool();
483 					} else {
484 						launchExternal();
485 					}
486     			}
487     		});
488     	}
489     	return openButton;
490     }
491     
492     private void openInCorasModellingTool() {
493     	if (!setOpenExternal(true)) {
494     		return;
495     	}
496     	CorasRepresentation theRepresentation = getRepresentation();
497     	if (theRepresentation == null || theRepresentation instanceof DgmModel) {
498     		DgmModel model = (DgmModel) theRepresentation;
499     		final String filename = model != null ? model.getFilename() : "model.dgm";
500     		String suffix = FileSuffixFilter.getSuffix(filename);
501     		suffix = suffix != null ? "." + suffix : "";
502     		String prefix = FileSuffixFilter.getPrefix(filename);
503 			try {
504 				final File tempFile = File.createTempFile(prefix, suffix);
505 				if (model != null) {
506 					byte[] bytes = XmlHelper.xmlToUtf8(model.getXmlContent(), true);
507 					IOUtils.saveFile(bytes, tempFile);
508 				}
509 				
510 				final MainFrame frame = new MainFrame();
511 				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
512 				frame.addWindowListener(new WindowAdapter() {
513 					public void windowClosed(WindowEvent e) {
514 						if (isEditable() && frame.getEditor().isModified()) {
515 							frame.getEditor().savefile();
516 							try {
517 								CorasRepresentation newRepresentation = RepresentationFactory.createRepresentation(type, tempFile, filename);
518 		                		if (newRepresentation instanceof UMLModelRepresentation) {
519 		                			xmiLight = XmlHelper.getRootElement(((UMLModelRepresentation)newRepresentation).getXmiLight());
520 		                		} else {
521 		                			xmiLight = null;
522 		                		}
523 								setRepresentation(newRepresentation);
524 							} catch (Exception e1) {
525 								e1.printStackTrace();
526 							}
527 						}
528 						setOpenExternal(false);
529 					}
530 				});
531 				if (model != null) {
532 					frame.getEditor().openfile(tempFile);
533 				} else {
534 					frame.setFileName(tempFile.getAbsolutePath());
535 				}
536 				frame.setVisible(true);
537 			} catch (IOException e1) {
538 				// TODO Auto-generated catch block
539 				e1.printStackTrace();
540 			}
541 
542     	}
543     }
544     
545     private void launchExternal() {
546         // FIXME launching external program
547     	if (!setOpenExternal(true)) {
548     		return;
549     	}
550         final CorasRepresentation theRepresentation = getRepresentation();
551 	    SwingWorker externalWorker = new SwingWorker() {
552             public Object construct() {
553                 String filename = theRepresentation.getFilename();
554                 String suffix = FileSuffixFilter.getSuffix(filename);
555                 suffix = suffix != null ? "." + suffix : "";
556                 String prefix = FileSuffixFilter.getPrefix(filename);
557                 byte[] bytes;
558                 if (theRepresentation instanceof BinaryRepresentation) {
559                     bytes = ((BinaryRepresentation)theRepresentation).getBinaryContent();
560                 } else if (theRepresentation instanceof XmlRepresentation) {
561                     bytes = XmlHelper.xmlToUtf8(((XmlRepresentation)theRepresentation).getXmlContent(), true);
562                 } else {
563                     return null;
564                 }
565                 try {
566                     File tempFile = File.createTempFile(prefix, suffix);
567                     IOUtils.saveFile(bytes, tempFile);
568                     long lastModified = tempFile.lastModified();
569                     
570                     AssociationService as = new AssociationService();
571 //                  Association assoc = as.getAssociationByContent(tempFile.toURI().toURL());
572                     Association assoc = as.getFileExtensionAssociation(suffix);
573                     System.out.println("actions: " + assoc.getActionList());
574                     Action a = assoc.getActionByVerb("edit");
575                     if (a == null) {
576                     	a = assoc.getActionByVerb("open");
577                     }
578                     String command = a.getCommand();
579                     command = command.replaceFirst("%1", tempFile.getAbsolutePath().replaceAll("////", "////////"));
580                     command = command.replaceFirst("%systemroot%", "C:////WINDOWS////");
581                     System.out.println("command: " + command);
582                     Process process = Runtime.getRuntime().exec(command);
583                     try {
584 						process.waitFor();
585 					} catch (InterruptedException e1) {
586 						// TODO Auto-generated catch block
587 						e1.printStackTrace();
588 					}
589 					int exitCode = process.exitValue();
590 					if (exitCode != 0) {
591 						System.err.println("abnormal termination: " + exitCode);
592 					}
593 					if (isEditable() && lastModified < tempFile.lastModified()) {
594 						try {
595 							CorasRepresentation newRepresentation = RepresentationFactory.createRepresentation(type, tempFile, filename);
596 	                		if (newRepresentation instanceof UMLModelRepresentation) {
597 	                			xmiLight = XmlHelper.getRootElement(((UMLModelRepresentation)newRepresentation).getXmiLight());
598 	                		} else {
599 	                			xmiLight = null;
600 	                		}
601 							setRepresentation(newRepresentation);
602 						} catch (Exception e) {
603 						}
604 					}
605 //                    Desktop.open(tempFile);
606                 } catch (IOException e) {
607                     e.printStackTrace();
608 //                } catch (DesktopException e) {
609 //                    e.printStackTrace();
610                 }
611                 return null;
612             }
613             public void finished() {
614             	setFinished(true);
615             	setOpenExternal(false);
616             }
617 	    };
618 	    externalWorker.start();
619     }
620 
621 	public boolean isOpenExternal() {
622 		return openExternal;
623 	}
624 	
625     private synchronized boolean setOpenExternal(boolean openExternal) {
626     	if (this.openExternal && openExternal) {
627     		return false; // only allow one application open at a time
628     	}
629     	this.openExternal = openExternal;
630     	updateButtons();
631     	return true;
632     }
633     
634     /***
635 	 * This method initializes jTextField	
636 	 * 	
637 	 * @return javax.swing.JTextField	
638 	 */    
639 	private JTextField getFileField() {
640 		if (fileField == null) {
641 			fileField = new JTextField();
642 			fileField.setEditable(false);
643 		}
644 		return fileField;
645 	}
646 	/***
647 	 * This method initializes jButton	
648 	 * 	
649 	 * @return javax.swing.JButton	
650 	 */    
651 	private JButton getImportButton() {
652 		if (importButton == null) {
653 			importButton = new JButton();
654 			importButton.setText("Import");
655 			importButton.setToolTipText("Import new content from file");
656 			importButton.setMnemonic(java.awt.event.KeyEvent.VK_R);
657 			importButton.setEnabled(false);
658 			importButton.addActionListener(new java.awt.event.ActionListener() { 
659 				public void actionPerformed(java.awt.event.ActionEvent ev) {
660 				    if (type == null) {
661 				        return; // FIXME error
662 				    }
663 				    CorasRepresentation theRepresentation = getRepresentation();
664 				    String filename = theRepresentation != null ? theRepresentation.getFilename() : null;
665 				    FileFilter[] fileFilters;
666 				    if (type == RepresentationTypeEnum.XMI_LIGHT) {
667 				    	fileFilters = IOUtils.mergeFilters(
668 				    			RepresentationTypeEnum.XMI_MODEL.getFileFilters(),
669 				    			IOUtils.mergeFilters(RepresentationTypeEnum.DGM_MODEL.getFileFilters(),
670 				    					RepresentationTypeEnum.ZUMLZARGO_MODEL.getFileFilters()));
671 				    } else {
672 				    	fileFilters = type.getFileFilters();
673 				    }
674 				    final File f = IOUtils.showOpenDialog(filename, fileFilters, importButton.getTopLevelAncestor());
675 				    if (f != null) {
676 						final Container owner = getTopLevelAncestor();
677 						owner.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
678 						final ProgressDialog dialog = new ProgressDialog(owner);
679 				        SwingWorker worker = new SwingWorker() {
680 				            public Object construct() {
681 				                try {
682 				                	setJobName("Importing " + f.getName());
683 				                	setCurrentTask("Importing " + f.getName());
684 				                	setTaskLength(type == RepresentationTypeEnum.XMI_LIGHT ? 2 : 1);
685 				                	CorasRepresentation newRepresentation = RepresentationFactory.createRepresentation(type, f);
686 				                	setProgress(1);
687 				                	if (type == RepresentationTypeEnum.XMI_LIGHT) {
688 				                		if (newRepresentation instanceof UMLModelRepresentation) {
689 				                			setCurrentTask("Converting UML model");
690 				                			xmiLight = XmlHelper.getRootElement(((UMLModelRepresentation)newRepresentation).getXmiLight());
691 				                		} else {
692 				                			xmiLight = null;
693 				                		}
694 				                		setProgress(2);
695 				                	}
696                                     setRepresentation(newRepresentation);
697                                 } catch (Exception e) {
698                                     // TODO Auto-generated catch block
699                                     e.printStackTrace();
700                                 }
701                                 return null;
702 				            }
703 				            public void finished() {
704 				            	setFinished(true);
705 				            	owner.setCursor(Cursor.getDefaultCursor());
706 				            }
707 				        };
708 				        dialog.setWorker(worker);
709 				        dialog.setCancellable(false);
710 				        worker.start();
711 				        dialog.setVisible(true);
712 				    }
713 				}
714 			});
715 		}
716 		return importButton;
717 	}
718 	/***
719 	 * This method initializes jPanel	
720 	 * 	
721 	 * @return javax.swing.JPanel	
722 	 */    
723 	private JPanel getContentPanel() {
724 		if (contentPanel == null) {
725 			contentPanel = new JPanel();
726 			contentPanel.setLayout(new CardLayout());
727 			contentPanel.add(getEmptyPanel(), getEmptyPanel().getName());
728 			contentPanel.add(getLoadingPanel(), getLoadingPanel().getName());
729 			contentPanel.add(getImagePanel(), getImagePanel().getName());
730 			contentPanel.add(getTextPanel(), getTextPanel().getName());
731 			contentPanel.add(getHtmlPanel(), getHtmlPanel().getName());
732 		}
733 		return contentPanel;
734 	}
735 	/***
736 	 * This method initializes jPanel	
737 	 * 	
738 	 * @return javax.swing.JPanel	
739 	 */    
740 	private JPanel getImagePanel() {
741 		if (imagePanel == null) {
742 			imagePanel = new JPanel(new GridBagLayout());
743 			imagePanel.setName("image");
744 		}
745 		return imagePanel;
746 	}
747 	/***
748 	 * This method initializes jPanel	
749 	 * 	
750 	 * @return javax.swing.JPanel	
751 	 */    
752 	private JPanel getTextPanel() {
753 		if (textPanel == null) {
754 			textPanel = new JPanel();
755 			textPanel.setLayout(new BorderLayout());
756 			textPanel.setName("text");
757 			textPanel.add(getTextArea(), java.awt.BorderLayout.CENTER);
758 		}
759 		return textPanel;
760 	}
761 	
762 	/***
763 	 * This method initializes jPanel	
764 	 * 	
765 	 * @return javax.swing.JPanel	
766 	 */    
767 	private JPanel getLoadingPanel() {
768 		if (loadingPanel == null) {
769 			loadingPanel = new JPanel();
770 			loadingPanel.setName("loading");
771 			loadingPanel.setLayout(new GridLayout(1, 1));
772 			loadingPanel.add(new JLabel("Loading ..."));
773 		}
774 		return loadingPanel;
775 	}
776 	/***
777 	 * This method initializes jPanel	
778 	 * 	
779 	 * @return javax.swing.JPanel	
780 	 */    
781 	private JPanel getEmptyPanel() {
782 		if (emptyPanel == null) {
783 			emptyPanel = new JPanel();
784 			emptyPanel.setName("empty");
785 		}
786 		return emptyPanel;
787 	}
788 	/***
789 	 * This method initializes jTextArea	
790 	 * 	
791 	 * @return javax.swing.JTextArea	
792 	 */    
793 	private CorasTextArea getTextArea() {
794 		if (textArea == null) {
795 			textArea = new CorasTextArea();
796 			textArea.setEditable(false);
797 		}
798 		return textArea;
799 	}
800 	
801 	private void updateButtons() {
802 		CorasRepresentation theRepresentation = getRepresentation();
803 		getImportButton().setEnabled(true);
804 		getExportButton().setEnabled(theRepresentation != null);
805 		getOpenButton().setEnabled(!isOpenExternal() &&
806 				(theRepresentation != null || type == RepresentationTypeEnum.XMI_LIGHT));
807 		getOpenButton().setText(isEditable() ? "Edit" : "Open");
808 		if (theRepresentation instanceof DgmModel 
809 				|| (theRepresentation == null && type == RepresentationTypeEnum.XMI_LIGHT)) {
810 			getOpenButton().setToolTipText("Open in CORAS UML modelling tool");
811 		} else {
812 			getOpenButton().setToolTipText("Open in external application");
813 		}
814 	}
815     
816 }