summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/graph/controller/DeletionController.java
blob: 38e8347ba61125e2f1a816237d1d1b9658bd2eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.c2kernel.graph.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;

import com.c2kernel.graph.event.SelectionChangedEvent;
import com.c2kernel.graph.model.DirectedEdge;
import com.c2kernel.graph.model.GraphModelManager;
import com.c2kernel.graph.model.Vertex;


// The deletion controller is responsible for deleting the present
// selection within the graph.
//
// The controller listens to:
//     * The graph model to determine if there is a selection
//     * The delete button
//     * The graph panel for the typing of the delete key
//
// The controller modifies:
//     * The graph model to delete the current selection
//     * The delete button to enable it only when there is a selection
public class DeletionController extends KeyAdapter implements Observer, ActionListener
{
    private GraphModelManager mGraphModelManager   = null;
    private JButton           mDeleteButton        = null;


    public void setGraphModelManager(GraphModelManager graphModelManager)
    {
        mGraphModelManager = graphModelManager;
        mGraphModelManager.addObserver(this);
    }


    public void setDeleteButton(JButton deleteButton)
    {
        mDeleteButton = deleteButton;
        mDeleteButton.addActionListener(this);
    }


    // Invoked by the graph model
    public void update(Observable o, Object arg)
    {
        SelectionChangedEvent event            = null;
        DirectedEdge          selectedEdge     = null;
        Vertex[]              selectedVertices = null;


        // If the selected edge has changed
        if(arg instanceof SelectionChangedEvent && mDeleteButton != null  && mGraphModelManager.isEditable())
        {
            // Enable the button if a single edge or single vertex is selected
            event            = (SelectionChangedEvent)arg;

            selectedEdge     = event.mSelection.mEdge;
            selectedVertices = event.mSelection.mVertices;

            mDeleteButton.setEnabled((selectedEdge != null) || (selectedVertices != null));
        }
    }


    // Invoked by the graph panel
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_DELETE && mGraphModelManager.isEditable())
        {
            mGraphModelManager.getModel().deleteSelection();
        }
    }


    // Invoked by the delete button
    public void actionPerformed(ActionEvent ae)
    {
        if(mGraphModelManager != null && mGraphModelManager.isEditable())
        {
            mGraphModelManager.getModel().deleteSelection();
        }
    }
}