// UML Model Transformation Tool (UMT)
// Copyright (C) 2003, 2004, 2005 SINTEF
// Authors: jon.oldevik at sintef.no | roy.gronmo at sintef.no | tor.neple at sintef.no | fredrik.vraalsen at sintef.no
// Webpage: http://umt.sourceforge.net
// Deloped in the projects: ACEGIS (EU project - IST-2002-37724),
// CAFE (EUREKA/ITEA - ip00004), FAMILIES (ITEA project ip02009)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2.1
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307 USA
package org.sintef.umt.umtmain;
/**
* @author Jon Oldevik, (jon.oldevik@sintef.no)
*
* @copyright (c) SINTEF 2002 (www.sintef.no)
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ResourceBundle;
import org.sintef.umt.hutntree.HutnEditor;
import org.sintef.umt.systemfamily.VariabilityEventListener;
import org.sintef.umt.systemfamily.VariabilityManager;
import org.sintef.umt.transformer.XMLUtility;
import org.sintef.umt.utils.UriViewer;
import org.sintef.umt.utils.UriViewerListener;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class PIMViewer extends JPanel implements
ActionListener,
org.sintef.umt.hutntree.HutnModelListener,
Runnable,
UriViewerListener,
WorkContextListener,
VariabilityEventListener
{
private final int MODE_INFO = 0;
private final int MODE_MODEL = 1;
private final int MODE_SYSTEM_FAMILY = 2;
private int _mode = MODE_INFO;
private HutnEditor hutneditor;
private XMLEditor hutntext;
private static OutputListener output;
private JButton hutntransform;
// private PropertyEditor _propertyeditor;
private String _hutntmpbuffer;
private volatile File _inputFile;
private volatile Reader _inputReader;
private volatile String _buffer;
private JSplitPane _viewersplit /*, _propertysplit */;
private UriViewer _infoviewer;
private UriViewerListener _viewerlistener = null;
private ResourceLoadListener _resourceloadlistener = null;
private final String propertyfile = UMTMain.config_dir + "properties.xml";
private final String xmi2xmilight = UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmi2xmilight");
private final String uml2xmi2xmilight = UMTMain.resource_dir + UMTMain.getResourceName("transformation.uml2xmi2xmilight");
private final String xmiactivity2xmilight = UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmiactivity");
private String _current_infopage;
private volatile boolean _isUML2 = false;
public PIMViewer (OutputListener outputlistener)
{
super (new BorderLayout ());
_viewersplit = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT);
// _propertysplit = new JSplitPane (JSplitPane.VERTICAL_SPLIT);
hutneditor = new HutnEditor (this);
hutntext = new XMLEditor ();
// hutntext.setContentType ("text/html");
hutntext.setToolTipText ("Area for display and edit of XMI-light text.");
_infoviewer = new UriViewer (false);
_infoviewer.setViewerListener(this);
_viewersplit.setLeftComponent(new JScrollPane(hutneditor));
_viewersplit.setRightComponent(hutntext);
_viewersplit.setDividerLocation(240);
_viewersplit.setResizeWeight(0.40);
_viewersplit.setDividerSize(5);
_viewersplit.setOneTouchExpandable(true);
_viewersplit.setContinuousLayout(true);
add (_viewersplit);
output = outputlistener;
_hutntmpbuffer = "";
JButton validate = new JButton (new ImageIcon(UMTMain.img_dir + "file.gif"));
validate.setActionCommand("validate");
validate.setContentAreaFilled(false);
validate.setBorderPainted (false);
validate.setOpaque (true);
validate.addActionListener(this);
validate.setToolTipText("Validate XMI Light contents.");
hutntext.addActionButton(validate);
JButton increaseFont = new JButton ("+");
increaseFont.setActionCommand("increaseFont");
increaseFont.setContentAreaFilled(false);
increaseFont.setBorderPainted (false);
increaseFont.setOpaque (true);
increaseFont.addActionListener(this);
increaseFont.setToolTipText("Increase XMI Light Font Size");
hutntext.addActionButton(increaseFont);
JButton decreaseFont = new JButton ("-");
decreaseFont.setActionCommand("decreaseFont");
decreaseFont.setContentAreaFilled(false);
decreaseFont.setBorderPainted (false);
decreaseFont.setOpaque (true);
decreaseFont.addActionListener(this);
decreaseFont.setToolTipText("Decrease XMI Light Font Size");
hutntext.addActionButton(decreaseFont);
}
public void setUriViewerListener (UriViewerListener listener){
_viewerlistener = listener;
}
public void setResourceLoadListener (ResourceLoadListener listener) {
_resourceloadlistener = listener;
}
public void setUMTMain (UMTMain umtmain) {
hutneditor.setUMTMain(umtmain);
setResourceLoadListener(umtmain);
}
/**
* Reloads properties and settings
*/
public void reload () {
// _propertyeditor.reload ();
}
public void colorTextLine(int lineNumber){
hutntext.colorLine(lineNumber);
}
public void clearLineColors(){
hutntext.clearLineColors();
}
private void updateInfoModePage () {
if (_current_infopage == null || _current_infopage == "") {
_infoviewer.setFile(UMTMain.docs_dir + WorkContext.getInitialStateInfoPage());
} else {
_infoviewer.setFile(UMTMain.docs_dir + _current_infopage);
}
}
public void setInfoMode () {
_mode = MODE_INFO;
_viewersplit.setRightComponent(_infoviewer);
updateInfoModePage ();
}
public void updateInfoMode () {
if (UMTMain.getWorkContext().getState() == WorkContext.STATE_INITIAL) {
// Disregard the current viewing mode: Always show the info mode
updateInfoModePage ();
_viewersplit.setRightComponent(_infoviewer);
}
else {
if (_mode == MODE_INFO)
setInfoMode ();
else if (_mode == MODE_MODEL)
setModelModeView ();
}
}
private void setModelModeView() {
_viewersplit.setRightComponent(hutntext);
}
public void setModelMode () {
_mode = MODE_MODEL;
updateInfoMode ();
}
public void setSystemFamilyMode () {
output.addLine("setSystemFamilyModel..");
_mode = MODE_SYSTEM_FAMILY;
// System.out.println ("PIMViewer::setSystemFamilyMode");
VariabilityManager varMgr = new VariabilityManager ();
Element modelroot = hutneditor.getModelRoot();
if (modelroot != null) {
varMgr.variabilityPanel(modelroot);
_viewersplit.setRightComponent(varMgr);
}
else {
System.out.println ("PIMViewer::setSystemFamilyMode - ModelRoot no set." );
varMgr = null;
}
}
public void updateInfo () {
}
public void run () {
String name = null;
try {
name = Thread.currentThread().getName();
if (name.equalsIgnoreCase("openXmiFile")) {
hutneditor.setLoading(true);
doXmi2HutnTransform (_inputFile, "");
hutneditor.setLoading(false);
} else if (name.equalsIgnoreCase("openSystemFamilyFile")) {
hutneditor.setLoading(true);
doXmi2HutnTransform (_inputFile, "system-family");
hutneditor.setLoading(false);
} else if (name.equalsIgnoreCase("openXmiReader") || name.equalsIgnoreCase("openSystemFamilyReader")) {
hutneditor.setLoading(true);
doXmi2HutnTransform(_inputReader, "system-family");
hutneditor.setLoading(false);
}else if (name.equalsIgnoreCase("openXmiReader") || name.equalsIgnoreCase("openSystemFamilyReader")) {
hutneditor.setLoading(true);
doXmi2HutnTransform(_inputReader, "system-family");
hutneditor.setLoading(false);
}else if (name.equalsIgnoreCase("openXmiLightFile")) {
hutneditor.setLoading(true);
try {
FileReader reader = new FileReader (_inputFile);
char [] data = new char[(int)_inputFile.length()];
reader.read(data);
hutneditor.openHutnReader(new StringReader (String.valueOf(data)), "");
hutntext.setStyledInput(String.valueOf(data), "XMI-Light representation");
} catch (Exception ex){
output.addLine ("Error reading file: " + _inputFile.getAbsolutePath() + " - " + ex.getMessage ());
}
XMLUtility util = new XMLUtility (output);
util.addLookupPath(UMTMain.resource_dir);
if (!UMTMain.environmentIsEmbedded())
util.doSAXParseValidate(hutntext.getReader());
hutneditor.setLoading(false);
} else if (name.equalsIgnoreCase("openXmiLightString")) {
hutneditor.setLoading(true);
Reader r = new StringReader (_buffer);
hutneditor.openHutnReader(r, "");
hutntext.setStyledInput(_buffer, "Reversed XMI Light");
XMLUtility util = new XMLUtility (output);
util.addLookupPath(UMTMain.resource_dir);
if (!UMTMain.environmentIsEmbedded())
util.doSAXParseValidate(hutntext.getReader());
hutneditor.setLoading(false);
}
setPositionForTextView(0);
} catch (Exception ex) {
hutneditor.setLoading(false);
} finally {
try {if (_inputReader != null)_inputReader.close();} catch(IOException ioex){System.out.println("PIMViewer::run::" + ioex);}
// Notify resourceloadlisteners finished loading
if (_resourceloadlistener != null)
_resourceloadlistener.resourceLoaded("Finished", name);
}
UMTMain.getWorkContext().setState(WorkContext.STATE_PERFORMED_ACTION);
}
public void setTransforming (boolean transforming) {
hutneditor.setTransforming(transforming);
}
public void closeView (){
hutneditor.closeView ();
hutntext.clear();
}
public void openXmiFile (File f, boolean isUml2){
_inputFile = f;
String cmd = "openXmiFile";
this._isUML2 = isUml2;
Thread t = new Thread (this, cmd);
t.start();
}
public void openXmiReader (Reader r) {
_inputReader = r;
Thread t = new Thread (this, "openXmiReader");
t.start();
}
public void openSystemFamilyReader (Reader r) {
_inputReader = r;
Thread t = new Thread (this, "openSystemFamilyReader");
t.start();
}
public void openSystemFamilyFile (File f, boolean isUml2) {
_inputFile = f;
String cmd = "openSystemFamilyFile";
this._isUML2 = isUml2;
Thread t = new Thread (this, cmd);
t.start();
}
/**
* Returns the input source.name
*/
public String getSource () {
if (_inputFile != null) {
return _inputFile.getAbsolutePath();
}
return "";
}
public String getHutnBuffer ()
{
if (_hutntmpbuffer != null && (!_hutntmpbuffer.equals("")))
return _hutntmpbuffer;
return hutntext.getContents ();
}
public void openHutnReader (String hutn) {
if (hutn != null) {
_inputFile = null;
_buffer = hutn;
Thread t = new Thread (this, "openXmiLightString");
t.start ();
}
}
public void openHutnFile (File f)
{
if (f != null) {
_inputFile = f;
Thread t = new Thread (this, "openXmiLightFile");
t.start();
}
}
/**
*
*
*/
public String parseHutnFile (File f) {
StringBuffer xmil = new StringBuffer ();
String line = null;
try {
BufferedReader breader = new BufferedReader (new FileReader (f));
line = breader.readLine();
while (line != null) {
xmil.append(line + "\r\n");
line = breader.readLine();
}
} catch (IOException ex) {
System.out.println ("PIMViewer::parseHutnFile - " + ex);
}
return xmil.toString();
}
public String getPSMBuffer () {
String psm = hutneditor.getTreeBuffer();
return psm;
}
public void setPositionForTextView (int position){
hutntext.setCaretPosition (position);
}
public void setPositionForTextView (String matchtext) {
hutntext.focusText (matchtext);
}
public void setPositionForTextView (Node node) {
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element)node;
String name = e.getAttribute("name");
String id = e.getAttribute("id");
String nodename = e.getNodeName();
if(id.equals("")) {
// try to find the first parent that has a name and an id
Node parent = e.getParentNode();
setPositionForTextView (parent);
} else {
hutntext.focusElement(name, id);
}
}
}
/**
*
* @param xmisource
*/
private void doXmi2HutnTransform (File xmisource, String type){
String hutn = xmiTransform (xmisource);
if (hutn != null && !(hutn.equals(""))) {
if (UMTMain.getUmtType() == UMTMain.UMT_SYSTEM_FAMILY) {
hutneditor.openHutnReader (new StringReader (hutn), type);
} else {
hutneditor.openHutnReader (new StringReader (hutn), "");
}
hutntext.setStyledInput(hutn, "XMI-Light representation");
}else {
// produce output?
}
}
/**
*
* @param xmisource
*/
private void doXmi2HutnTransform (Reader xmisource, String type){
String hutn = xmiTransform (xmisource);
if (hutn != null && !(hutn.equals(""))) {
if (UMTMain.getUmtType() == UMTMain.UMT_SYSTEM_FAMILY) {
hutneditor.openHutnReader (new StringReader (hutn), type);
} else {
hutneditor.openHutnReader (new StringReader (hutn), "");
}
hutntext.setStyledInput(hutn, "XMI-Light representation");
}
}
public String xmiTransform (File xmisource) {
String hutn = null;
try {
File xsltfile = null;
if (!_isUML2)
xsltfile = new File (xmi2xmilight);
else
xsltfile = new File (uml2xmi2xmilight);
// TODO : remove copyResourcesToTarget
// copyResourcesToTarget (xmisource);
XMLUtility xmlutility = new XMLUtility(output);
xmlutility.addLookupPath(UMTMain.resource_dir);
hutn = xmlutility.transform(xmisource, xsltfile);
// if (!UMTMain.environmentIsEmbedded())
xmlutility.doSAXParseValidate(new StringReader(hutn));
xmlutility = null;
xsltfile = null;
} catch (Exception ex) {
output.addLine("Error occured in XMI transformation." + ex.getMessage());
}
/* System.gc(); */
return hutn;
}
/**
* @param xmisource
* @return
*/
public String xmiTransform (Reader xmisource) {
String hutn = null;
try {
File xsltfile = new File (xmi2xmilight);
XMLUtility xmlutility = new XMLUtility(output);
xmlutility.addLookupPath(UMTMain.resource_dir);
if (xsltfile.getParentFile() != null)
xmlutility.addLookupPath(xsltfile.getParentFile().getAbsolutePath());
Reader xsltreader = null;
try {
xsltreader = new FileReader (xsltfile);
} catch (IOException ioex) {
}
hutn = xmlutility.transform(xmisource, xsltreader);
xmlutility.doSAXParseValidate(new StringReader(hutn));
xmlutility = null; xsltfile = null;
} catch (Exception ex) {
}
return hutn;
}
/**
* xmiLight2xmi
*
*/
public void xmiLight2xmi(Writer xmiwriter) {
try {
Reader xmilight = hutntext.getReader();
String xslt = UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmilight2xmi");
File xsltfile = new File (xslt);
FileReader xsltreader = new FileReader (xsltfile);
XMLUtility xmlutility = new XMLUtility (output);
xmlutility.transform(xmilight, xsltreader, xmiwriter);
} catch (Exception ex) {
System.out.println (ex);
}
}
/**
*
*
*
*/
private void copyResourcesToTarget (File target) {
/*
System.out.println("copyResourcesToTarget:" + target.getAbsolutePath());
String resourcename = "uml13.dtd";
String resource = UMTMain.resource_dir + resourcename;
File resourcefile = new File (resource);
File parent = target.getParentFile();
if (parent != null) System.out.println("Parent:" + parent.getAbsolutePath());
if (parent != null && parent.isDirectory()) {
System.out.println ("Parent exists and is directory.");
File targetresource = new File (parent + System.getProperty("file.separator") + resourcename);
if (!targetresource.exists()) {
try {
String command = "copy " + "\"" + resourcefile.getAbsolutePath() + "\"" + " " + "\"" + targetresource.getAbsolutePath() + "\"";
Runtime.getRuntime().exec (command, null);
System.out.println ("Runtime.exec: " + command);
} catch (Exception ex) {
System.out.println ("PIMViewer::copyResourceToTarget " + ex);
}
}
}
*/
}
public String doTransform (Reader source, Reader transformsource)
{
StringWriter resultwriter = new StringWriter ();
XMLUtility xmlutility = null;
try{
xmlutility = new XMLUtility (output);
String result = xmlutility.transform(source, transformsource);
return result;
} catch (Exception ex) {
System.out.println ("doTransform::" + ex);
}
xmlutility = null;
return resultwriter.toString ();
}
public void actionPerformed (ActionEvent ae){
JButton source = (JButton)ae.getSource ();
if (source.getActionCommand().equals("xml")){
source.setActionCommand ("pim");
_hutntmpbuffer = hutntext.getContents ();
String nonxml = "";
try{
nonxml = doTransform (new StringReader(_hutntmpbuffer),
new FileReader(UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmilight2xmilight")));
} catch (IOException ioex){
System.out.println (ioex);
}
hutntext.setHutnInput (nonxml, "'PIM' Representation");
} else if (source.getActionCommand().equalsIgnoreCase("validate")) {
output.clear();
XMLUtility xmlutility = new XMLUtility (output);
xmlutility.addLookupPath(UMTMain.resource_dir);
xmlutility.doSAXParseValidate(hutntext.getReader());
} else if (source.getActionCommand().equalsIgnoreCase("increaseFont")){
hutntext.increaseFont();
} else if (source.getActionCommand().equalsIgnoreCase("decreaseFont")) {
hutntext.decreaseFont();
} else {
source.setActionCommand ("xml");
hutntext.setStyledInput(_hutntmpbuffer, "XML-light Representation");
_hutntmpbuffer= "";
}
// Buffer the current hutn
}
//
// HutnModelListener methods
//
public void viewChanged (String description, Node modelnode)
{
// System.out.println ("PIMViewer::viewChanged");
String nodename = "", stereotype ="", id = "";
nodename = modelnode.getNodeName ();
try {
stereotype = ((Element)modelnode).getAttribute("stereoType");
id = ((Element)modelnode).getAttribute ("id");
} catch (Exception ex){}
// Update the properties
/*
PropertyGroup group = _propertyeditor.getPropertyManager().getRoot().getSubgroupForStereotypedItem(nodename, stereotype, "");
_propertyeditor.setProperties(group);
*/
// Update the 'textual view
// setPositionForTextView ("id=\"" + id + "\"");
setPositionForTextView (modelnode);
}
private class PimViewerMouseAdapter extends MouseAdapter{
public void mouseClicked (MouseEvent me){
System.out.println ("PIMViewer::mouseClicked" );
}
}
/**
* Implementation of UriViewerEvent interface
* @param event
*/
public void viewerEvent (String event) {
System.out.println ("PIMViewer::viewerEvent:" + event);
if (event.equalsIgnoreCase("umt:XMILight")) {
setModelMode ();
} else if (event.equalsIgnoreCase("umt:resolve")) {
// Resolve the variability
} else if (event.equalsIgnoreCase("umt.check")) {
// Check the variability model
}
if (_viewerlistener != null)
_viewerlistener.viewerEvent(event);
}
/**
* Implementation of WorkContextListener interface
*/
public void stateChanged (WorkContext context) {
// System.out.println ("stateChanged: " + context.getState() + " " + context.getStateDescription() + " " + context.getStateInfoPage());
int state = context.getState();
_current_infopage = context.getStateInfoPage();
updateInfoMode ();
if (state == WorkContext.STATE_PERFORMED_ACTION) {
if (UMTMain.getUmtType() == UMTMain.UMT_SYSTEM_FAMILY) {
setSystemFamilyMode ();
}
}
}
/**
* Implementation of V
* @param changedObject
*/
public void variabilityModelChanged (Object changedObject) {
// update the treeview to reflect updates...
hutneditor.repaint();
}
}