2020-11-21 19:46:43 +01:00
import javax.swing.* ;
import java.awt.* ;
2020-11-22 23:34:44 +01:00
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
2020-11-21 19:46:43 +01:00
2020-11-22 15:55:26 +01:00
/ * *
* Affiche une barre d ' actions en haut de la fenêtre .
* @author V . BOULANGER
* /
2020-12-18 22:54:42 +01:00
public class UmlToolbar extends JPanel {
2020-11-22 23:34:44 +01:00
2020-11-23 15:44:41 +01:00
public static final int SELECT_TOOL = 0 ;
public static final int EDIT_TOOL = 1 ;
public static final int DELETE_TOOL = 2 ;
public static final int COPY_TOOL = 3 ;
public static final int PASTE_TOOL = 4 ;
public static final int CLASS_TOOL = 5 ;
2020-12-20 18:04:40 +01:00
public static final int STRONG_TOOL = 6 ;
public static final int WEAK_TOOL = 7 ;
public static final int COMPOSITION_TOOL = 8 ;
public static final int AGGREGATION_TOOL = 9 ;
public static final int INHERITANCE_TOOL = 10 ;
2020-11-23 15:44:41 +01:00
private int _currentTool = 0 ;
2020-11-22 23:34:44 +01:00
private JPanel _editionPanel ;
private JButton _select ;
private JButton _edit ;
private JButton _delete ;
private JButton _copy ;
private JButton _paste ;
private JButton _undo ;
private JButton _redo ;
private JPanel _modelisationPanel ;
private JButton _newClass ;
2020-12-20 18:04:40 +01:00
private JButton _newStrong ;
private JButton _newWeak ;
private JButton _newComposition ;
private JButton _newAggregation ;
private JButton _newInheritance ;
2020-11-22 23:34:44 +01:00
2020-12-07 22:18:54 +01:00
private UmlDiagram _umlDiagram ;
2020-11-22 23:34:44 +01:00
2020-11-21 19:46:43 +01:00
/ * *
* Constructeur de la classe AppThinkerToolbar
2020-12-09 12:48:52 +01:00
* @param diagram Le diagramme UML auquel appartient la toolbar .
2020-11-21 19:46:43 +01:00
* /
2020-12-18 22:54:42 +01:00
public UmlToolbar ( UmlDiagram diagram ) {
2020-12-07 22:18:54 +01:00
_umlDiagram = diagram ;
2020-11-21 19:46:43 +01:00
//Création de la Toolbar
2020-12-07 22:18:54 +01:00
this . setLayout ( new GridLayout ( 2 , 2 , 10 , 0 ) ) ;
2020-11-21 19:46:43 +01:00
this . setBackground ( new Color ( 69 , 69 , 72 ) ) ;
2020-11-22 23:34:44 +01:00
_editionPanel = new JPanel ( ) ;
_editionPanel . setLayout ( new GridLayout ( 1 , 7 ) ) ;
_select = new JButton ( ) ;
_select . setSize ( 32 , 32 ) ;
_select . setBorderPainted ( false ) ;
_select . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/select.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_select . setToolTipText ( " Select item. " ) ;
2020-11-22 23:34:44 +01:00
_select . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . SELECT_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_editionPanel . add ( _select ) ;
_edit = new JButton ( ) ;
_edit . setSize ( 32 , 32 ) ;
_edit . setBorderPainted ( false ) ;
_edit . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/edit.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_edit . setToolTipText ( " Edit item properties. " ) ;
2020-11-22 23:34:44 +01:00
_edit . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . EDIT_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_editionPanel . add ( _edit ) ;
_delete = new JButton ( ) ;
_delete . setSize ( 32 , 32 ) ;
_delete . setBorderPainted ( false ) ;
_delete . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/delete.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_delete . setToolTipText ( " Delete item. " ) ;
2020-11-22 23:34:44 +01:00
_delete . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . DELETE_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_editionPanel . add ( _delete ) ;
_copy = new JButton ( ) ;
_copy . setSize ( 32 , 32 ) ;
_copy . setBorderPainted ( false ) ;
_copy . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/copy.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_copy . setToolTipText ( " Copy item. " ) ;
2020-11-22 23:34:44 +01:00
_copy . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . COPY_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_editionPanel . add ( _copy ) ;
_paste = new JButton ( ) ;
_paste . setSize ( 32 , 32 ) ;
_paste . setBorderPainted ( false ) ;
_paste . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/paste.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_paste . setToolTipText ( " Paste item. " ) ;
2020-11-22 23:34:44 +01:00
_paste . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . PASTE_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_editionPanel . add ( _paste ) ;
_undo = new JButton ( ) ;
_undo . setSize ( 32 , 32 ) ;
_undo . setBorderPainted ( false ) ;
_undo . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/undo.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_undo . setToolTipText ( " Undo last change. " ) ;
2020-11-22 23:34:44 +01:00
_editionPanel . add ( _undo ) ;
_redo = new JButton ( ) ;
_redo . setSize ( 32 , 32 ) ;
_redo . setBorderPainted ( false ) ;
_redo . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/redo.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_redo . setToolTipText ( " Redo last change. " ) ;
2020-11-22 23:34:44 +01:00
_editionPanel . add ( _redo ) ;
_modelisationPanel = new JPanel ( ) ;
_modelisationPanel . setLayout ( new GridLayout ( 1 , 3 ) ) ;
_newClass = new JButton ( ) ;
_newClass . setSize ( 32 , 32 ) ;
_newClass . setBorderPainted ( false ) ;
_newClass . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/newClass.png " ) ) ) ;
2020-12-07 22:18:54 +01:00
_newClass . setToolTipText ( " Add a new class. " ) ;
2020-11-22 23:34:44 +01:00
_newClass . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-18 22:54:42 +01:00
setCurrentTool ( UmlToolbar . CLASS_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
_modelisationPanel . add ( _newClass ) ;
2020-12-20 18:04:40 +01:00
_newStrong = new JButton ( ) ;
_newStrong . setSize ( 32 , 32 ) ;
_newStrong . setBorderPainted ( false ) ;
_newStrong . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/strongRelation.png " ) ) ) ;
_newStrong . setToolTipText ( " <html>Add a <b>strong relation</b>.<br>A <b>strong relation</b> is a relation that will always be verified.<br><i>Eg: Teachers teach courses.</i></html> " ) ;
_newStrong . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
setCurrentTool ( UmlToolbar . STRONG_TOOL ) ;
}
} ) ;
_modelisationPanel . add ( _newStrong ) ;
_newWeak = new JButton ( ) ;
_newWeak . setSize ( 32 , 32 ) ;
_newWeak . setBorderPainted ( false ) ;
_newWeak . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/weakRelation.png " ) ) ) ;
_newWeak . setToolTipText ( " <html>Add a <b>weak relation</b>.<br>A <b>weak relation</b> is a relation that only exists at a particular time.<br><i>Eg: A person has a job when he works for a company.</i></html> " ) ;
_newWeak . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
setCurrentTool ( UmlToolbar . WEAK_TOOL ) ;
}
} ) ;
_modelisationPanel . add ( _newWeak ) ;
_newComposition = new JButton ( ) ;
_newComposition . setSize ( 32 , 32 ) ;
_newComposition . setBorderPainted ( false ) ;
_newComposition . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/compositionRelation.png " ) ) ) ;
_newComposition . setToolTipText ( " <html>Add a <b>composition relation</b>.<br>It corresponds to the verb <b>'to have'</b>, <b>with the uniqueness</b> of the component side.<br><i>Eg: A caddie have 4 wheels.</i></html> " ) ;
_newComposition . addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e ) {
setCurrentTool ( UmlToolbar . COMPOSITION_TOOL ) ;
}
} ) ;
_modelisationPanel . add ( _newComposition ) ;
_newAggregation = new JButton ( ) ;
_newAggregation . setSize ( 32 , 32 ) ;
_newAggregation . setBorderPainted ( false ) ;
_newAggregation . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/aggregationRelation.png " ) ) ) ;
_newAggregation . setToolTipText ( " <html>Add an <b>aggregation relation</b>.<br>It corresponds to the verb <b>'to have'</b>, <b>without the uniqueness</b> of the component side.<br><i>Eg: A room has 4 walls. (One wall may be the wall of another room).</i></html> " ) ;
_newAggregation . addActionListener ( new ActionListener ( ) {
2020-11-22 23:34:44 +01:00
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-20 18:04:40 +01:00
setCurrentTool ( UmlToolbar . AGGREGATION_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
2020-12-20 18:04:40 +01:00
_modelisationPanel . add ( _newAggregation ) ;
_newInheritance = new JButton ( ) ;
_newInheritance . setSize ( 32 , 32 ) ;
_newInheritance . setBorderPainted ( false ) ;
_newInheritance . setIcon ( new ImageIcon ( getClass ( ) . getResource ( " img/x32/inheritanceRelation.png " ) ) ) ;
_newInheritance . setToolTipText ( " <html>Add an <b>inheritance relation</b>.<br>A class is <b>a specification</b> of another class.<br><i>Eg: An employer is a person.</i></html> " ) ;
_newInheritance . addActionListener ( new ActionListener ( ) {
2020-11-22 23:34:44 +01:00
@Override
public void actionPerformed ( ActionEvent e ) {
2020-12-20 18:04:40 +01:00
setCurrentTool ( UmlToolbar . INHERITANCE_TOOL ) ;
2020-11-22 23:34:44 +01:00
}
} ) ;
2020-12-20 18:04:40 +01:00
_modelisationPanel . add ( _newInheritance ) ;
2020-11-22 23:34:44 +01:00
this . add ( _editionPanel , BorderLayout . CENTER ) ;
this . add ( _modelisationPanel , BorderLayout . CENTER ) ;
2020-12-07 22:18:54 +01:00
JLabel editLabel = new JLabel ( " Edition " ) ;
2020-11-21 19:46:43 +01:00
editLabel . setHorizontalAlignment ( JLabel . CENTER ) ;
editLabel . setForeground ( Color . WHITE ) ;
this . add ( editLabel ) ;
2020-12-07 22:18:54 +01:00
JLabel modelisationLabel = new JLabel ( " Modelisation " ) ;
2020-11-21 19:46:43 +01:00
modelisationLabel . setHorizontalAlignment ( JLabel . CENTER ) ;
modelisationLabel . setForeground ( Color . WHITE ) ;
this . add ( modelisationLabel ) ;
2020-11-22 23:34:44 +01:00
2020-12-07 22:18:54 +01:00
this . setEnabled ( false ) ;
2020-11-22 23:34:44 +01:00
}
/ * *
2020-12-07 22:18:54 +01:00
* Verrouille / Déverrouille la barre d ' outil .
* @param enabled Paramètre de verrouillage .
2020-11-22 23:34:44 +01:00
* /
2020-12-07 22:18:54 +01:00
public void setEnabled ( boolean enabled ) {
_select . setEnabled ( enabled ) ;
_edit . setEnabled ( enabled ) ;
_delete . setEnabled ( enabled ) ;
_copy . setEnabled ( enabled ) ;
_paste . setEnabled ( enabled ) ;
_undo . setEnabled ( enabled ) ;
_redo . setEnabled ( enabled ) ;
_newClass . setEnabled ( enabled ) ;
2020-12-20 18:04:40 +01:00
_newStrong . setEnabled ( enabled ) ;
_newWeak . setEnabled ( enabled ) ;
_newComposition . setEnabled ( enabled ) ;
_newAggregation . setEnabled ( enabled ) ;
_newInheritance . setEnabled ( enabled ) ;
2020-11-22 23:34:44 +01:00
}
/ * *
2020-11-29 18:56:04 +01:00
* Récupère l ' outil actuellement en fonction .
* @return L ' outil actuellement en fonction .
2020-11-22 23:34:44 +01:00
* /
2020-11-29 18:56:04 +01:00
public int getCurrentTool ( ) {
return this . _currentTool ;
2020-11-22 23:34:44 +01:00
}
/ * *
2020-11-23 15:44:41 +01:00
* Change d ' outil pour l ' édition du diagramme .
2020-11-29 18:56:04 +01:00
* @param currentTool L ' outil cible .
2020-11-22 23:34:44 +01:00
* /
2020-11-29 18:56:04 +01:00
public void setCurrentTool ( int currentTool ) {
this . _currentTool = currentTool ;
2020-12-18 22:54:42 +01:00
if ( this . _currentTool = = UmlToolbar . SELECT_TOOL ) _umlDiagram . setCursor ( new Cursor ( Cursor . DEFAULT_CURSOR ) ) ;
2020-12-07 22:18:54 +01:00
else _umlDiagram . setCursor ( new Cursor ( Cursor . CROSSHAIR_CURSOR ) ) ;
2020-11-29 18:56:04 +01:00
switch ( currentTool ) {
2020-12-20 18:04:40 +01:00
case EDIT_TOOL :
2020-12-07 22:18:54 +01:00
if ( _umlDiagram . getSelected ( ) instanceof Class ) {
_umlDiagram . editClass ( ( Class ) _umlDiagram . getSelected ( ) ) ;
}
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Edit tool - Click an item to edit its properties. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case DELETE_TOOL :
2020-12-07 22:18:54 +01:00
if ( _umlDiagram . getSelected ( ) instanceof Class ) {
_umlDiagram . removeClass ( ( Class ) _umlDiagram . getSelected ( ) ) ;
2020-11-29 18:56:04 +01:00
}
2020-12-07 22:18:54 +01:00
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Delete tool - Click an item to delete it. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case COPY_TOOL :
2020-12-07 22:18:54 +01:00
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Copy tool - Click an item to copy it. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case PASTE_TOOL :
2020-12-07 22:18:54 +01:00
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Paste tool - Click everywhere to paste the last copied item. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case CLASS_TOOL :
2020-12-07 22:18:54 +01:00
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Class tool - Click everywhere to add a new class. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case STRONG_TOOL :
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Strong relation tool - Click on a class, hold, and release on another class. " ) ;
break ;
case WEAK_TOOL :
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Weak relation tool - Click on a class, hold, and release on another class. " ) ;
break ;
case COMPOSITION_TOOL :
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Composition relation tool - Click on a class, hold, and release on another class. " ) ;
break ;
case AGGREGATION_TOOL :
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Aggregation relation tool - Click on a class, hold, and release on another class. " ) ;
2020-11-23 15:44:41 +01:00
break ;
2020-12-20 18:04:40 +01:00
case INHERITANCE_TOOL :
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Inheritance relation tool - Click on a class, hold, and release on another class. " ) ;
2020-11-23 15:44:41 +01:00
break ;
default :
2020-12-07 22:18:54 +01:00
AppThinker . getWindow ( ) . getStatusbar ( ) . setStatusMessage ( " Select tool - Click an item to select it or move it. " ) ;
2020-11-23 15:44:41 +01:00
break ;
}
2020-12-18 22:54:42 +01:00
_umlDiagram . repaint ( ) ;
2020-11-21 19:46:43 +01:00
}
}