summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/tabs/DomainPathAdmin.java
blob: 08c784eba7102e2da57833d7dd9994bee564262a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.c2kernel.gui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.c2kernel.common.ObjectNotFound;
import com.c2kernel.entity.proxy.ItemProxy;
import com.c2kernel.gui.MainFrame;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.process.Gateway;

/**************************************************************************
 *
 * $Revision: 1.3 $
 * $Date: 2004/10/21 08:02:21 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

public class DomainPathAdmin extends Box implements ActionListener {

    ItemProxy entity;
    JTable table;
    DomainPathTableModel model;
    JButton addButton;
    JButton removeButton;

    public DomainPathAdmin() {
        super(BoxLayout.Y_AXIS);

        model = new DomainPathTableModel(this);
        table = new JTable(model);
        add(new JScrollPane(table));

        add(Box.createVerticalGlue());
        Box buttonBox = Box.createHorizontalBox();
        addButton = new JButton("Add");
        buttonBox.add(addButton);
        buttonBox.add(Box.createHorizontalGlue());
        removeButton = new JButton("Remove");
        buttonBox.add(removeButton);
        buttonBox.add(Box.createHorizontalGlue());
        add(buttonBox);

        addButton.setActionCommand("add");
        addButton.addActionListener(this);
        removeButton.setActionCommand("remove");
        removeButton.addActionListener(this);
    }

    public void setEntity(ItemProxy entity) {
        this.entity = entity;
        model.loadPaths();
    }

   @Override
public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("add")) {
           String newPath = JOptionPane.showInputDialog(this, "Enter new path,", "Add Domain Path", JOptionPane.PLAIN_MESSAGE);
           addDomainPath(new DomainPath(newPath));
           model.loadPaths();
       }
       else if (e.getActionCommand().equals("remove")) {
           if (table.getSelectedRow() > -1) {
               DomainPath oldPath = model.getPath(table.getSelectedRow());
               removeDomainPath(oldPath);
               model.loadPaths();
           }
       }
   }

    public boolean removeDomainPath(DomainPath oldPath) {
        return alterDomainPath(oldPath, "Remove");
    }

    public boolean addDomainPath(DomainPath newPath) {
        return alterDomainPath(newPath, "Add");
    }

    public boolean alterDomainPath(DomainPath path, String action) {

        if (JOptionPane.showConfirmDialog(this,
                action+" "+path+"?",
                action+" Domain Path",
                JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
            return false;

        String[] params = new String[1];
        params[0] = path.toString();
        try {
            MainFrame.userAgent.execute(entity, action+"DomainPath", params);
        } catch (Exception e) {
        	MainFrame.exceptionDialog(e);
            return false;
        }
        return true;
    }

    private class DomainPathTableModel extends AbstractTableModel {
        ArrayList<DomainPath> domPaths;
        DomainPathAdmin parent;
        public DomainPathTableModel(DomainPathAdmin parent) {
            this.parent = parent;
            domPaths = new ArrayList<DomainPath>();
        }

        public void loadPaths() {
            domPaths.clear();
            for (Iterator<?> currentPaths = Gateway.getLookup().search(new DomainPath(), entity.getName()); currentPaths.hasNext();) {
                DomainPath thisPath = (DomainPath)currentPaths.next();
                try {
					if (thisPath.getItemPath().equals(entity.getPath())) domPaths.add(thisPath);
				} catch (ObjectNotFound e) { }
            }
            fireTableDataChanged();
        }

        public DomainPath getPath(int rowIndex) {
            return domPaths.get(rowIndex);
        }
        @Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            DomainPath oldPath = domPaths.get(rowIndex);
            DomainPath newPath = new DomainPath((String)aValue);
            boolean success = parent.addDomainPath(newPath);
            if (success)
                success = parent.removeDomainPath(oldPath);
            if (success) {
                oldPath.setPath(newPath);
                fireTableDataChanged();
            }
        }

        @Override
		public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
		public int getColumnCount() {
            return 1;
        }

        @Override
		public String getColumnName(int column) {
            return "Path";
        }

        @Override
		public int getRowCount() {
            return domPaths.size();
        }

        @Override
		public Object getValueAt(int rowIndex, int columnIndex) {
            return domPaths.get(rowIndex).toString();
        }

        @Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }
    }
}