// 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 org.sintef.umt.propertyeditor.PropertyEditor;
import org.sintef.umt.propertyeditor.PropertyGroup;
import org.sintef.umt.propertyeditor.PropertyManager;
import org.sintef.umt.umtmain.PIMViewer;
import org.sintef.umt.umtmain.UMTMain;
import org.sintef.umt.utils.UmtFileFilter;
import java.util.*;
import java.io.File;
public class ProjectManager implements ListSelectionListener, ActionListener, KeyListener
{
private final String _projectfile = UMTMain.config_dir + "projects.xml";
private final String _default_location = "workarea";
private PropertyManager _propertymanager;
private PropertyEditor _propertyeditor;
private JList _projectlist;
private Vector _project_ids;
private ProjectEditor _current_project;
private Component _dialog_owner;
private JDialog _dialog;
private Vector _projectlisteners;
private PIMViewer _pimviewer;
public ProjectManager (Component dialog_owner, PIMViewer pimviewer) {
_propertymanager = new PropertyManager (_projectfile);
_propertyeditor = new PropertyEditor (_propertymanager);
_propertyeditor.setNameEditable(false);
_dialog_owner = dialog_owner;
_projectlisteners = new Vector ();
_projectlist = new JList ();
_project_ids = new Vector ();
/* _dialog = new JDialog ();*/
_pimviewer = pimviewer;
}
public void addProjectListener (ProjectListener listener) {
if (listener != null) {
_projectlisteners.add(listener);
}
}
public void notifyListeners (String notification) {
String name, location, id;
if (_current_project != null) {
name = _current_project.getName();
location = _current_project.getLocation();
id = _current_project.getId();
} else
return;
for (Enumeration _enum = _projectlisteners.elements();_enum.hasMoreElements();) {
ProjectListener listener = (ProjectListener) _enum.nextElement();
if (notification.equalsIgnoreCase("close")) {
listener.projectClosed(name, id);
} else if (notification.equalsIgnoreCase("open")) {
listener.projectOpened(name, location, id);
} else if (notification.equalsIgnoreCase("save")) {
listener.projectSaved(name, location, id);
} else if (notification.equalsIgnoreCase("new")) {
listener.projectNew (name, location, id);
}
}
}
public boolean hasOpenProject () {
return (_current_project != null);
}
public String getCurrentProjectName () {
if (_current_project != null) {
return _current_project.getName();
}
return "";
}
public String getCurrentProjectId () {
if (_current_project != null) {
return _current_project.getId();
}
return "";
}
public String getSelectedProject () {
String project = (String)_projectlist.getSelectedValue();
return project;
}
public String getSelectedProjectId () {
String id = "";
try {
id = (String) _project_ids.elementAt(_projectlist.getSelectedIndex());
} catch (Exception ex) {
}
return id;
}
/**
* Show create project dialog -
* Create a new project and set the current working context
*
*/
public void createProjectDialog () {
createProjectDialog ("New project", "", "", "xmi");
}
/**
* Starts a user dialog to create create a new project
*/
public void createProjectDialog (String name, String location, String source, String sourcetype) {
final JTextField namefield = new JTextField (16);
final JTextField locfield = new JTextField (16);
// final JTextField sourcefield = new JTextField (16);
final JTextField sourcetypefield = new JTextField (16);
// final JComboBox sourcebox = new JComboBox ();
final JTextField sourcefield = new JTextField ();
final JDialog localdialog = new JDialog ((JFrame)_dialog_owner, "Create a new project", true);
localdialog.setLocation((int)(_dialog_owner.getX() * 1.1) , (int) (_dialog_owner.getY() * 1.1));
localdialog.setSize (new Dimension (350, 140));
JLabel plocation = new JLabel ("Project Location");
JLabel pname = new JLabel ("Project Name");
JLabel psource = new JLabel ("Source File");
JLabel psourcetype = new JLabel ("sourcetype");
final JButton browse = new JButton ("Set project location");
// browse.setEnabled(true);
final JButton addsource = new JButton ("Add source");
final JButton ok = new JButton ("Ok");
final JButton cancel = new JButton ("Cancel");
// final JButton create_and_load = new JButton ("Create and load");
final JFileChooser browser = new JFileChooser ("");
browser.setCurrentDirectory (new File(System.getProperty("user.dir")));
browser.setFileFilter(new org.sintef.umt.utils.UmtFileFilter());
ActionListener createlistener = new ActionListener (){
boolean confirmed = false;
public void actionPerformed (ActionEvent ae) {
String action = ae.getActionCommand();
Object srcbtn = ae.getSource();
if (srcbtn == browse) {
browser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
confirmed = false;
boolean cancelled = false;
while (!confirmed && !cancelled) {
int ret = browser.showOpenDialog(localdialog);
if(ret == JFileChooser.APPROVE_OPTION) {
String selected;
File f = browser.getSelectedFile();
selected = f.getPath();
if (ProjectEditor.projectExists(selected)) {
int res = JOptionPane.showConfirmDialog(_dialog_owner, "The selected directory already contains a project file - is it ok to overwrite?");
confirmed = res==JOptionPane.OK_OPTION? true: false;
} else
confirmed = true;
browser.setCurrentDirectory(f);
locfield.setText (selected);
locfield.setToolTipText(selected);
/*
if (sourcefield.getText() == null || sourcefield.getText().equals(""))
sourcefield.setText(selected);
*/
} else
cancelled = true;
}
} else if (srcbtn == addsource) {
confirmed = true;
browser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
UmtFileFilter filter = new UmtFileFilter ();
browser.setFileFilter(filter);
int ret = browser.showOpenDialog(localdialog);
if (ret == JFileChooser.APPROVE_OPTION) {
String selected = browser.getSelectedFile().getAbsolutePath();
sourcefield.setText(selected);
sourcefield.setToolTipText(selected);
if (locfield.getText() == null || locfield.getText().equals("")) {
locfield.setText(browser.getSelectedFile().getParent());
locfield.setToolTipText(locfield.getToolTipText() + " : " + locfield.getText());
if (ProjectEditor.projectExists(browser.getSelectedFile().getParent())) {
int res = JOptionPane.showConfirmDialog(_dialog_owner, "The selected directory already contains a project file - is it ok to overwrite?");
if (res == JOptionPane.CANCEL_OPTION) {
confirmed = false;
browse.doClick();
}
}
}
browser.setCurrentDirectory(browser.getSelectedFile());
}
} else if (srcbtn == ok) {
String name = namefield.getText();
String loc = locfield.getText();
String [] sources = new String [1];
sources[0] = sourcefield.getText();
boolean projectExists = ProjectEditor.projectExists(locfield.getText());
if (name == null || name.equals("")){
JOptionPane.showMessageDialog(localdialog, "Missing project name");
return;
} else if (projectExists && ! confirmed) {
int res = JOptionPane.showConfirmDialog(_dialog_owner, "The selected directory already contains a project file - is it ok to overwrite?");
confirmed = res==JOptionPane.OK_OPTION? true: false;
}
if (confirmed || !projectExists){
if (projectExists)
ProjectManager.this.deleteProjectByLocation(loc);
PropertyManager mgr = ProjectEditor.getPropertiesForLocation(loc);
localdialog.setVisible(false);
createProject (name, loc, sources, sourcetypefield.getText());
notifyListeners ("New");
}
} else if (srcbtn == cancel) {
// cancel the creation
localdialog.setVisible(false);
}
}
};
namefield.setText(name);
namefield.setToolTipText("The name of the new project.");
locfield.setText(location);
locfield.setToolTipText("The location of the new project, i.e. a directory location - this is defined by the location of the source file.");
locfield.setEditable(true);
sourcefield.setText(source);
sourcefield.setToolTipText(source);
sourcetypefield.setText(sourcetype); sourcetypefield.setToolTipText(sourcetype);
browse.addActionListener(createlistener);
ok.addActionListener(createlistener);
cancel.addActionListener(createlistener);
addsource.addActionListener(createlistener);
Container c = localdialog.getContentPane();
c.setLayout(new BorderLayout ());
JPanel inputpanel = new JPanel (new GridLayout (3,3));
JPanel buttonpanel = new JPanel (new FlowLayout (FlowLayout.LEFT));
inputpanel.add(pname);inputpanel.add(namefield);inputpanel.add(new JLabel(""));
inputpanel.add(plocation);inputpanel.add(locfield);inputpanel.add(browse);
inputpanel.add(psource); inputpanel.add(sourcefield); inputpanel.add(addsource);
buttonpanel.add (ok); buttonpanel.add(cancel);
c.add(inputpanel, BorderLayout.CENTER);
c.add(buttonpanel, BorderLayout.SOUTH);
localdialog.setVisible (true);
}
public void createProject (String name, String location, String[] sources, String sourcetype) {
try {
_current_project = new ProjectEditor (name, location, _dialog_owner, this, true);
if (_current_project.getInitiated()) {
String id = _current_project.getId();
_current_project.setSources(sources);
_propertymanager.createPropertyGroup(id, name, "project", name, "");
String [] property = new String [4];
if (location == null || location.equals ("")) {
location = _default_location + System.getProperty("file.separator") + name;
}
property [0] = "location"; property[1] = location;
_propertymanager.setPropertyForSubgroup(id, property);
_propertymanager.storeProperties();
initProjectList ();
}
} catch (Exception ex) {
}
}
private void initProjectList () {
try {
Vector newdata = new Vector ();
_project_ids.clear();
for (Iterator projects = _propertymanager.getPropertyGroups();projects.hasNext();) {
PropertyGroup pg = (PropertyGroup)projects.next();
newdata.add(pg.getItem());
_project_ids.add(pg.getId());
}
_projectlist.setListData(newdata);
_projectlist.revalidate();
_projectlist.setSelectedIndex(_projectlist.getModel().getSize() - 1);
int index = _projectlist.getSelectedIndex();
if (index > -1) {
valueChanged ((String)_project_ids.elementAt(index));
}
} catch (Exception ex) {
System.out.println ("ProjectEditor::initProjectList - " + ex);
ex.printStackTrace();
}
}
/**
* Selecy a project among existing ones.
* Sets the current working context to the openned project
*/
public void selectProject (String action) {
// show existing projects
_dialog = new JDialog ();
Container c = _dialog.getContentPane();
c.setLayout(new BorderLayout ());
JPanel viewpanel = new JPanel (new BorderLayout ());
JPanel buttonpanel = new JPanel (new FlowLayout (FlowLayout.LEFT));
initProjectList ();
setPreferredColors (_propertyeditor);
_projectlist.addListSelectionListener(this);
_projectlist.addKeyListener(this);
// _projectlist.setPreferredSize(new Dimension(100,50));
_projectlist.setToolTipText (action); // Uses tooltip to communicate the action
setPreferredColors (_projectlist);
viewpanel.add(new JScrollPane(_projectlist), BorderLayout.WEST);
viewpanel.add(_propertyeditor, BorderLayout.CENTER);
JButton btn = new JButton ("Ok");
btn.setToolTipText(action);
btn.addActionListener (this);
btn.setActionCommand(action);
buttonpanel.add(btn);
btn = new JButton ("Cancel");
btn.addActionListener (this);
buttonpanel.add(btn);
c.add(viewpanel, BorderLayout.CENTER);
c.add(buttonpanel, BorderLayout.SOUTH);
_dialog.setSize(new Dimension (500,320));
java.awt.Point loc = _dialog_owner.getLocation();
/* _dialog.setLocationRelativeTo(_dialog_owner); */ // Not supported in jdk 1.3.1
_dialog.setLocation((int)loc.getX() + 200, (int)loc.getY() + 200);
_dialog.setTitle("Select project to " + action);
_dialog.setModal(true);
_dialog.setVisible (true);
}
private void setPreferredColors (JComponent comp){
comp.setForeground(Color.black);
comp.setBackground(Color.white);
}
/**
*
* Saves the current working context
*
*/
public void saveProject () {
if (_current_project != null) {
// String psm = _pimviewer.getPSMBuffer();
// String psm_name = _current_project.getPSM();
String location = _current_project.getLocation();
/*try {
File psmfile = new File (location + System.getProperty("file.separator") + psm_name);
java.io.FileWriter writer = new java.io.FileWriter (psmfile);
writer.write(psm);
writer.close();
} catch (java.io.IOException ioex) {
System.out.println ("ProjectManager::saveProject" + ioex);
}
*/
/*
String hutnbuffer = _pimviewer.getHutnBuffer();
String pim_name = _current_project.getPIM();
try {
File pimfile = new File (location + System.getProperty("file.separator") + pim_name);
java.io.FileWriter writer = new java.io.FileWriter (pimfile);
writer.write(hutnbuffer);
writer.close();
} catch (java.io.IOException ioex) {
System.out.println ("ProjectManager::saveProject" + ioex);
}
*/
_current_project.saveProject();
notifyListeners ("save");
}
}
public void deleteProject (String id) {
_propertymanager.removePropertyGroup(id);
_propertymanager.storeProperties();
}
/**
* Deletes an existing project based on name and location
* Saves the properties when done.
*/
public void deleteProjectByName (String project, String location) {
Iterator it = _propertymanager.getPropertyGroups();
for (;it.hasNext();){
PropertyGroup projectgroup = (PropertyGroup)it.next();
String current_location = projectgroup.getProperty("location");
if (projectgroup.getName().equals(project) && current_location.equalsIgnoreCase(location)) {
// this project matches....
// _propertymanager.removePropertyGroup(project, project, "");
_propertymanager.removePropertyGroup(projectgroup);
initProjectList ();
_propertymanager.storeProperties();
break;
}
}
// TODO: Delete Project file .umtproject
try {
} catch (Exception ioex) {
}
}
/**
* Deletes an existing project based on its location
* Saves the properties when done.
*/
public void deleteProjectByLocation (String location) {
Iterator it = _propertymanager.getPropertyGroups();
for (;it.hasNext();){
PropertyGroup projectgroup = (PropertyGroup)it.next();
String current_location = projectgroup.getProperty("location");
if (current_location.equalsIgnoreCase(location)) {
_propertymanager.removePropertyGroup(projectgroup);
initProjectList ();
_propertymanager.storeProperties();
break;
}
}
}
public void showCurrentProject () {
if (_current_project != null) {
_current_project.setVisible(true);
}
}
public ProjectEditor getCurrentProject () {
if (_current_project != null) {
return _current_project;
}else
return null;
}
public void doOpenProject (String project_id) {
PropertyGroup project = _propertymanager.getPropertyGroup(project_id);
if (project != null) {
Map props = project.getProperties();
String location = ((String[]) props.get("location"))[0];
_current_project = new ProjectEditor (project.getName(), location, _dialog_owner, this, false);
// _current_project.setVisible(true);
notifyListeners ("open");
}
}
public boolean findProjectWithSource (File source) {
try {
String sname = source.getAbsolutePath();
System.out.println ("findProjectWithSource: " + sname);
PropertyManager project_properties;
PropertyGroup group;
for (Iterator projects = _propertymanager.getPropertyGroups(); projects.hasNext();) {
PropertyGroup project = (PropertyGroup) projects.next();
Map props = project.getProperties();
String location = ((String[])props.get("location"))[0];
if (ProjectEditor.projectExists(location)) {
project_properties = ProjectEditor.getPropertiesForLocation(location);
if (project_properties != null) {
group = project_properties.getPropertyGroupForItem("files");
String projectsource = group.getProperty("source");
System.out.println ("comparing with: " + projectsource);
if (projectsource.equalsIgnoreCase(sname)) {
doOpenProject (project.getId());
notifyListeners ("open");
return true;
}
}
}
}
} catch (Exception ex) {
}
return false;
}
public void setCurrentProject (ProjectEditor projecteditor) {
_current_project = projecteditor;
}
public void closeProject () {
notifyListeners ("close");
_current_project = null;
}
public void handleAction (String action) {
if (action.equalsIgnoreCase("open")) {
_dialog.setVisible(false);
_propertymanager.storeProperties();
doOpenProject (getSelectedProjectId ());
} else if (action.equalsIgnoreCase ("cancel")) {
_dialog.setVisible(false);
} else if (action.equalsIgnoreCase ("delete")) {
String project = getSelectedProject ();
String project_id = getSelectedProjectId ();
if (JOptionPane.showConfirmDialog(_dialog_owner, "Project '" + project + "' will be deleted. Are you sure?") == JOptionPane.OK_OPTION)
deleteProject (project_id);
_dialog.setVisible(false);
} else if (action.equalsIgnoreCase ("create")) {
createProjectDialog ();
}
}
public void actionPerformed (ActionEvent ae) {
String action = ae.getActionCommand ();
handleAction (action);
}
public void valueChanged(ListSelectionEvent e) {
try {
String value = (String)_projectlist.getSelectedValue();
int index = _projectlist.getSelectedIndex();
if (index > -1) {
String id = (String) _project_ids.elementAt(index);
valueChanged (id);
}
} catch (Exception ex) {
System.out.println ("ProjectEditor::valueChanged - " + ex);
}
}
private void valueChanged (String id) {
_propertyeditor.setPropertiesForGroupId(id);
if (_dialog != null)
_dialog.repaint ();
}
/*
* KeyListener methods
*
* keypressed fetches the action performed from the ToolTip property
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
JComponent source = (JComponent)e.getSource();
handleAction (source.getToolTipText());
} else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
handleAction ("cancel");
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
}