summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/graph/controller/StartVertexController.java
blob: 0dc959c984a97f3771c625d75128232a08db38f8 (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
package com.c2kernel.graph.controller;

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

import javax.swing.JButton;

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


// The start vertex controller is responsible for selecting
// the vertex at the start of the graph.
//
// The controller listens to:
//     * The graph model to determine if there is a single
//       vertex selected
//     * The start vertex button
//
// The controller modifies:
//     * The graph model to select the start vertex
//     * The start button to enable it only when there is a
//       single vertex selected
public class StartVertexController implements Observer, ActionListener
{
    private GraphModelManager mGraphModelManager  = null;
    private JButton           mStartButton        = null;


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


    public void setStartButton(JButton startButton)
    {
        mStartButton = startButton;
        mStartButton.addActionListener(this);
    }


    public void update(Observable o, Object arg)
    {
        SelectionChangedEvent event            = null;
        Vertex[]              selectedVertices = null;

        // If the selected vertex has changed
        if(arg instanceof SelectionChangedEvent && mStartButton != null)
        {
            event            = (SelectionChangedEvent)arg;
            selectedVertices = event.mSelection.mVertices;

            if(selectedVertices == null)
            {
                mStartButton.setEnabled(false);
            }
            else if (mGraphModelManager.isEditable())
            {
                mStartButton.setEnabled(selectedVertices.length == 1);
            }
        }
    }


    public void actionPerformed(ActionEvent ae)
    {
        if(mGraphModelManager != null)
        {
            mGraphModelManager.getModel().setSelectedVertexToBeStart();
        }
    }
}