Package org.sintef.umt.project

Source Code of org.sintef.umt.project.ProjectEditor

// 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.project;

/**
* @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 javax.swing.event.*;
import java.util.*;

import java.io.*;
import javax.swing.border.*;

import org.sintef.umt.propertyeditor.PropertyEditor;
import org.sintef.umt.propertyeditor.PropertyGroup;
import org.sintef.umt.propertyeditor.PropertyGroupEditorListener;
import org.sintef.umt.propertyeditor.PropertyManager;
import org.sintef.umt.umtmain.UMTMain;


public class ProjectEditor implements ListSelectionListener, ActionListener, PropertyGroupEditorListener
{

  private final static String _project_template = UMTMain.config_dir + "project_template.xml";
  private final static String _project_filename = ".umtproject";   
  private String _name, _location;
  private PropertyManager _propertymanager;
  private PropertyEditor _propertyeditor;
  private JDialog _dialog;
  private JPanel _panel;
  private JList _propertylist, _transformationlist;
  private JLabel _notelabel;
  private Component _owner;
  private static ProjectManager _projectmanager;
  private boolean _initiated;
 
  public ProjectEditor (String projectname, String location, Component owner, ProjectManager manager, boolean create)
  {
    _name = projectname;
    _location = location;
    _owner = owner;
    _projectmanager = manager;
    init (create);
    _propertylist.setSelectedIndex(0);
  }
 
 
  public static boolean projectExists (String location) {
    File locdir = new File(location);
    File project_file = new File (locdir.getAbsolutePath() + System.getProperty("file.separator") + _project_filename);
    return project_file.exists();
  }
  public static PropertyManager getPropertiesForLocation (String location) {
    if (projectExists (location)) {
      return new PropertyManager (location + System.getProperty("file.separator") + _project_filename);
    }
    return null;
  }
 
  public String getName () {
    return _name;
  }
 
  public String getLocation () {
    return _location;
  }
 
  public String getId () {
    String id = _propertymanager.getRoot().getId();
    return id;
  }

  public void setSources (String[] sources) {
    for (int i = 0; i < sources.length; i++) {           
      this.setPropertyForGroupItem ("Sources", "source_" + i, sources[i], "file", "");
    }
  } 
 
  public String[] getSources () {
    Collection source_collection = getPropertiesForGroupItem ("Sources");
    String [] valueobject = null;   
    String [] sources = new String[source_collection.size()];
    int index = 0;
    for (Iterator it = source_collection.iterator(); it.hasNext();) {
      valueobject = (String[]) it.next();
      sources[index++] = valueobject[0];
    }
    return sources;
  }
 
  public void addSource (String source) {
    Collection source_collection = getPropertiesForGroupItem ("Sources");
    setPropertyForGroupItem ("Sources", "source_" + source_collection.size(), source, "file", "");
  }
 
  // TODO: implement
  public void removeSource (String source) {
    // Remove a source...   
  }
 
  /**
   * Adds a transformation to this project
   * @param transformation
   */
  public void addTransformation (String transformation) {
    setPropertyForGroupItem ("Transformations", transformation, transformation, "", "");
  }
  /**
   * Removes a transformation from this project
   * @param transformation
   */
  public void removeTransformation (String transformation) {
    String[] property = new String[4]; property[0] = transformation;
    _propertymanager.removeProperty("Transformations", "", property);       
  }
 
  /**
   * @return a list of transformations for this project
   */
  public String [] getTransformations () {
    Map props = _propertymanager.getPropertiesForStereotypedItem("Transformations", "");
    String[] transformations = new String[props.size()];
    Collection col = props.values();
    int i = 0;
    for (Iterator it = col.iterator(); it.hasNext();) {
      String [] property = (String[]) it.next();
      transformations[i++] = property[0];
    }
    return transformations;
  }
   
  public String getPSM () {
    return getPropertyForGroupItem ("Files", "psm");
  }
 
  public void setPSM (String psm) {
    setPropertyForGroupItem ("Files", "psm", psm, "", "")
  }
   
  public String getPIM () {
    return getPropertyForGroupItem ("Files", "pim");
  }
 
  public void setPIM (String pim) {
    setPropertyForGroupItem ("Files", "pim", pim, "", "")
 
 
  public String getGenDir () {
    return getPropertyForGroupItem ("Dirs","gendir");
  }
  public void setGenDir (String gendir) {
    setPropertyForGroupItem ("Dirs", "gendir", gendir, "file", "");
  }
 
  public String getUserPropertiesFile () {
    return getPropertyForGroupItem ("Variables Output File","filename");
  }
 
  private void initProfile () {
    String [] profiles = UMTMain.getProfileNames();
    String value_domain = "none";
    for (int i = 0; i < profiles.length; i++) {
      value_domain += ", " + profiles[i];
    }
    String[] property = new String[4];
    property[0] = "Profile name";
    property[1] = "none";
    property[2] = "";
    property[3] = value_domain;
    _propertymanager.setProperty("Profile", "", property);
  }
 
  /**
   *
   * @return user variable properties
   */
  public PropertyGroup getUserProperties () {
    PropertyGroup pg = _propertymanager.getPropertyGroupForItem("User Variables");
    return pg;
  }
 
  private void setPropertyForGroupItem (String group, String property_name, String value, String type, String value_domain) {
    String [] property = new String [4];
    property [0] = property_name; property[1] = value; property[2] = type; property[3] = value_domain;
    _propertymanager.setProperty(group, "", property);
    _propertymanager.storeProperties();       
  }
 
  private String getPropertyForGroupItem (String group, String property_name) {
    PropertyGroup pg = _propertymanager.getPropertyGroupForItem(group);
    String property_value = pg.getProperty(property_name);
    return property_value;   
  }
 
  /**
   * Returns all properties for a specific group
   * @param group
   * @return
   */
  private Collection getPropertiesForGroupItem (String group) {
    PropertyGroup pg = _propertymanager.getPropertyGroupForItem(group);
    return pg.getProperties().values();
  }
     
  /**
   *  Initiate layout
   */
  private void init (boolean create) {
    if (initProject (create)) {
      initPanel ();
      initDialog ();   
      _initiated = true;     
    } else {
      _initiated = false;
      JOptionPane.showMessageDialog(null, "Could not initate project");
    }
  }
 
  /**
   * Initiates dialog for project editor
   */
  private void initDialog () {
    _dialog = new JDialog ();
    _dialog.setTitle("Project " + _name + "  [location:" + _location + "]");
    java.awt.Point loc = _owner.getLocation();
   /* _dialog.setLocationRelativeTo(_dialog_owner); */ // Not supported in jdk 1.3.1
    _dialog.setLocation((int)loc.getX() + 200, (int)loc.getY() + 200);     
    _dialog.setLocation(200,200);
    _dialog.getContentPane().add(_panel);
    _dialog.setModal(true);
    _dialog.setSize(_panel.getSize());   
  }
 
  /**
   *  Initiates panel for project editor
   */
  private void initPanel () {
    _panel = new JPanel (new BorderLayout ());
     
    Iterator props = _propertymanager.getPropertyGroups();
    Vector propvector = new Vector ();
    for (;props.hasNext();) {
      propvector.add(((PropertyGroup)props.next()).getItem());
    }     
    _propertylist = new JList (propvector);
    _propertylist.addListSelectionListener(this);
    _propertylist.setPreferredSize(new Dimension (100,70));     
    _propertyeditor = new PropertyEditor (_propertymanager);
    _propertyeditor.setNameEditable(false);   
    setPreferredColors(_propertyeditor);
    setPreferredColors(_propertylist);
     
    _notelabel = new JLabel ("");
     
    JPanel viewpanel = new JPanel (new BorderLayout ());
    JPanel buttonpanel = new JPanel (new FlowLayout (FlowLayout.LEFT));
    viewpanel.add(new JScrollPane(_propertylist), BorderLayout.WEST);
    viewpanel.add(_propertyeditor, BorderLayout.CENTER);
    viewpanel.add(_notelabel, BorderLayout.NORTH);
    JButton btn = new JButton ("Save");   
    btn.addActionListener (this);
    buttonpanel.add(btn);
    btn = new JButton ("Close");
    btn.addActionListener (this);
    buttonpanel.add(btn);   
    _panel.add(viewpanel, BorderLayout.CENTER);
    _panel.add(buttonpanel, BorderLayout.SOUTH);
    _panel.setSize(new Dimension (560,280));     
  }
 
  /**
   *  Initiates a different panel for project editor
   */
  public JPanel getProjectViewer () {
    JPanel le_panel = new JPanel (new GridLayout (2,1));
     
    _propertylist.setFont(new Font ("Default", Font.PLAIN, 12));
    Iterator props = _propertymanager.getPropertyGroups();
    Vector propvector = new Vector ();
    for (;props.hasNext();) {
      propvector.add(((PropertyGroup)props.next()).getItem());
    }           
   
    JPanel propertiespanel = new JPanel (new BorderLayout ());
    propertiespanel.add(new JScrollPane(_propertylist), BorderLayout.WEST);
    propertiespanel.add(_propertyeditor, BorderLayout.CENTER);
       
    JPanel actionpanel = new JPanel (new GridLayout (1,3));   
    JPanel transformpanel = new JPanel (new GridLayout (2,1));
    transformpanel.setBorder (new TitledBorder(new EtchedBorder(), "Transformations"));
    actionpanel.add(transformpanel);
   
    JPanel tmp = new JPanel (new GridLayout (2,2));
    transformpanel.add(tmp);
    JLabel l = new JLabel ("Available");
    tmp.add(l);
   
    l = new JLabel ("Current");
    tmp.add(l);   
   
    _transformationlist = new JList (UMTMain.getTransformationNames());
    _transformationlist .setVisibleRowCount(4);   
    _transformationlist .setForeground(Color.black);
    _transformationlist .setBackground(Color.white);
    tmp.add(new JScrollPane(_transformationlist ));   
   
    final JList elist = new JList (this.getTransformations());
    elist.setVisibleRowCount(4);
    elist.setForeground(Color.black);
    elist.setBackground(Color.pink);
    tmp.add(new JScrollPane (elist));
   
    ActionListener tactionlistener = new ActionListener () {
      public void actionPerformed (ActionEvent ae) {
        String action = ae.getActionCommand();
        if (action.equalsIgnoreCase("Add Transformation")) {
          int selected = _transformationlist .getSelectedIndex();
          if (selected > -1) {
            // add to transformations.
            addTransformation ((String) _transformationlist .getSelectedValue());
            elist.setListData(getTransformations());
          }
        } else if (action.equalsIgnoreCase("Remove transformation")) {
          if (elist.getSelectedIndex() > -1) {
            removeTransformation ((String)elist.getSelectedValue());
            elist.setListData(getTransformations());
          }         
        }
      }
    };
    tmp = new JPanel (new FlowLayout (FlowLayout.LEFT));
    transformpanel.add(tmp);
       
    JButton btn = new JButton ("Add transformation");
    btn.addActionListener(tactionlistener);
    tmp.add(btn);
   
    btn = new JButton ("Remove transformation");
    btn.addActionListener(tactionlistener);
    tmp.add(btn);
   
    JPanel sourcepanel = new JPanel (new FlowLayout());
    sourcepanel.setBorder (new TitledBorder(new EtchedBorder(), "Source"));
    actionpanel.add(sourcepanel);   
   
    btn = new JButton ("Set source");
    btn.addActionListener(this);
    sourcepanel.add(btn);
   
    JPanel executepanel = new JPanel ();
    executepanel.setBorder (new TitledBorder(new EtchedBorder(), "Execute"));
    actionpanel.add (executepanel);
   
    btn = new JButton ("Execute transformations");
    btn.addActionListener(this);
    executepanel.add(btn);   
             
    le_panel.add(propertiespanel);
    le_panel.add(actionpanel);   
   
    return le_panel;     
 
 
  /**
   * Sets the preferred colors for 'comp'
   * @param comp
   */
 
  private void setPreferredColors (JComponent comp){
    comp.setForeground(Color.black);
    comp.setBackground(Color.lightGray);
 
 
  public boolean getInitiated () {
    return _initiated;
  }
 
  public void setVisible (boolean visible) {
    if (visible) {
      _dialog.setVisible (true);
    } else {
      _dialog.setVisible(false);     
    }
  } 
 
  /**
   *
   *  Opens an existing location or creates it doesn't exist
   *
   */
  public boolean initProject (boolean create) {
    try {
      if (createLocation (_location, create)) {         
        return true;
      } else {
        System.out.println ("ProjectEditor::Failed to create project");
      }
    } catch (Exception ex) {
      System.out.println ("ProjectEditor::initProject - " + ex);
    }
    return false;
  }
 
  /**
   *  Checks if a project location is valid, i.e.
   *   if the directory exists and a project-file is there
   *
   */
  public boolean checkLocation (String location)
  {
    try {
      File loc = new File (location);     
      if (loc.exists()) {
        // look for a project file       
      } else {
      }       
     
    } catch (Exception ex) {
      System.out.println ("ProjectManager::checkLocation - " + ex);
    }   
    return false;
  }
 
  public boolean createLocation (String location, boolean create) {
    try {
      boolean success = org.sintef.umt.utils.FileUtils.createDirectory(location);
      File loc = new File (location);
      if (success && loc.exists()) {
        File project_file = new File (loc.getAbsolutePath() + System.getProperty("file.separator") + _project_filename);
        if (!initProjectFile (project_file, create))
          return false;
        else
          return true;
      }
    } catch (Exception ex) {
      // TODO: Add error handling
    }
    return false;
  }
 
  /**
   *  Initiates the project file - always from scratch...
   * @param project_file
   * @return
   */
  private boolean initProjectFile (File project_file, boolean create) {
    try {   
      if (!project_file.exists()) {
        project_file.createNewFile();
      }     
      _propertymanager = new PropertyManager (project_file);
      if (create) {
        String [] property = new String [4];
        property[2] =""; property[3] = "";     
        java.util.Calendar cal = java.util.Calendar.getInstance();
        _propertymanager.clear()
        _propertymanager.reinit ();     
        PropertyManager template = new PropertyManager (_project_template);
        _propertymanager.copy(template);         
        property[0] = "Created"; property[1] = cal.getTime().toString();
        _propertymanager.setProperty("Timestamps","", property);
        property[0] = "Last opened"; property[1] = cal.getTime().toString();
        _propertymanager.setProperty("Timestamps","", property);
        initProfile();     
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
    return true;
  }
 
  public void saveProject () {
    try {
      String [] property = new String[4];
      property[2] =""; property[3] = "";     
      property[0] = "Last Saved"; property[1] = Calendar.getInstance().getTime().toString();
      _propertymanager.setProperty("Timestamps", "", property);       
      _propertymanager.storeProperties();           
      _notelabel.setText("Project has been saved.");     
    } catch (Exception ex) {
      System.out.println ("ProjectEditor::saveProject - " + ex);
    }
  }
 
  public void valueChanged (ListSelectionEvent e) {
    Object source = e.getSource();
    if (source == _propertylist) {
      String value = (String)_propertylist.getSelectedValue();
      _propertyeditor.setPropertiesForGroup(value);
      _panel.repaint();     
    } else {
      //
    }
  }
 
  public void actionPerformed (ActionEvent ae) {
    String action = ae.getActionCommand();
    if (action.equalsIgnoreCase ("save")) {
      _projectmanager.saveProject();
    } else if(action.equalsIgnoreCase ("close")) {
      // TBD: check if saving is needed
      setVisible (false);
    }
  }
 
  public void notifyChanged (String action){ 
    _transformationlist.setListData(UMTMain.getTransformationNames());   
  }

}
 
TOP

Related Classes of org.sintef.umt.project.ProjectEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.