summaryrefslogtreecommitdiff
path: root/java/src/IceGridGUI/Application/ApplicationEditor.java
blob: 474d676ed7c918ffbdd43f1e63edade6fa6aa190 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGridGUI.Application;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;

import IceGrid.*;
import IceGridGUI.*;

class ApplicationEditor extends Editor
{
    protected boolean applyUpdate()
    {
	Root root = (Root)_target;
	MainPane mainPane = _target.getCoordinator().getMainPane();

	root.disableSelectionListener();
	try
	{
	    if(isSimpleUpdate())
	    {
		writeDescriptor();
		root.updated();
		root.getEditable().markModified();
	    }
	    else
	    {
		//
		// Save to be able to rollback
		//
		ApplicationDescriptor savedDescriptor = root.saveDescriptor();
		writeDescriptor();
		try
		{
		    root.rebuild();
		}
		catch(UpdateFailedException e)
		{
		    root.restoreDescriptor(savedDescriptor);
		    JOptionPane.showMessageDialog(
			root.getCoordinator().getMainFrame(),
			e.toString(),
			"Apply failed",
			JOptionPane.ERROR_MESSAGE);
		    return false;
		}
		//
		// Success
		//
		root.updated();
		root.getEditable().markModified();
		
		if(!savedDescriptor.name.equals(root.getId()))
		{
		    mainPane.resetTitle(root);
		}
	    }
	      
	    _applyButton.setEnabled(false);
	    _discardButton.setEnabled(false);
	    return true;
	}
	finally
	{
	    root.enableSelectionListener();
	}
    }

    ApplicationEditor()
    {
	_name.getDocument().addDocumentListener(_updateListener);
	_description.getDocument().addDocumentListener(_updateListener);
	
	//
	// Variables
	//
	_variables = new MapField(this, "Name", "Value", false);

	//
	// Distrib
	//
	_distrib = new JComboBox(new Object[]{NO_DISTRIB, DEFAULT_DISTRIB});
	_distrib.setEditable(true);
	_distrib.setToolTipText("The proxy to the IcePatch2 server holding your files");

	JTextField distribTextField = (JTextField)
	    _distrib.getEditor().getEditorComponent();
	distribTextField.getDocument().addDocumentListener(
	    _updateListener);

	_distribDirs.getDocument().addDocumentListener(_updateListener);
	_distribDirs.setToolTipText(
	    "<html>Include only these directories when patching.<br>"
	    + "Use whitespace as separator; use double-quotes around directories containing whitespaces</html>");

    }
 
    protected void appendProperties(DefaultFormBuilder builder)
    {    
	builder.append("Name");
	builder.append(_name, 3);

	builder.append("Description");
	builder.nextLine();
	builder.append("");
	builder.nextRow(-2);
	CellConstraints cc = new CellConstraints();
	JScrollPane scrollPane = new JScrollPane(_description);
	builder.add(scrollPane, 
		    cc.xywh(builder.getColumn(), builder.getRow(), 3, 3));
	builder.nextRow(2);
	builder.nextLine();

	builder.append("Variables");
	builder.nextLine();
	builder.append("");
	builder.nextLine();
	builder.append("");
	builder.nextLine();
	builder.append("");
	builder.nextRow(-6);
	scrollPane = new JScrollPane(_variables);
	builder.add(scrollPane, 
		    cc.xywh(builder.getColumn(), builder.getRow(), 3, 7));
	builder.nextRow(6);
	builder.nextLine();

	JComponent c = builder.appendSeparator("Distribution");
	c.setToolTipText("Files shared by all servers in your application");
	builder.append("IcePatch2 Proxy");
	builder.append(_distrib, 3);
	builder.nextLine();
	builder.append("Directories");
	builder.append(_distribDirs, 3);
	builder.nextLine();
    }
    
    protected void buildPropertiesPanel()
    {
	super.buildPropertiesPanel();
	_propertiesPanel.setName("Application Properties");
    }

    boolean isSimpleUpdate()
    {
	ApplicationDescriptor descriptor = (ApplicationDescriptor)_target.getDescriptor();
	return descriptor.name.equals(_name.getText().trim()) 
	    && _variables.get().equals(descriptor.variables);
    }

    void writeDescriptor()
    {
	ApplicationDescriptor descriptor = (ApplicationDescriptor)_target.getDescriptor();
	descriptor.name = _name.getText().trim();
	descriptor.variables = _variables.get();
	descriptor.description = _description.getText();

	if(_distrib.getSelectedItem() == NO_DISTRIB)
	{
	    descriptor.distrib.icepatch = "";
	}
	else
	{
	    descriptor.distrib.icepatch = _distrib.getSelectedItem().toString().trim();
	}
	descriptor.distrib.directories = _distribDirs.getList();
    }	    
    
    protected boolean validate()
    {
	return check(new String[]{"Name", _name.getText().trim()});
    }

    void show(Root root)
    {
	detectUpdates(false);
	_target = root;
	
	Utils.Resolver resolver = getDetailResolver();
	boolean isEditable = (resolver == null);

	ApplicationDescriptor descriptor = 
	    (ApplicationDescriptor)root.getDescriptor();

	_name.setText(descriptor.name);
	_name.setEditable(!root.isLive());

	_description.setText(
	    Utils.substitute(descriptor.description, resolver));
	_description.setEditable(isEditable);
	_description.setOpaque(isEditable);
	_description.setToolTipText("An optional description for this application");
	
	_variables.set(descriptor.variables, resolver, isEditable);
	
	_distrib.setEnabled(true);
	_distrib.setEditable(true);
	String icepatch = 
	    Utils.substitute(descriptor.distrib.icepatch, resolver);
	if(icepatch.equals(""))
	{
	    _distrib.setSelectedItem(NO_DISTRIB);
	}
	else
	{
	    _distrib.setSelectedItem(icepatch);
	}
	_distrib.setEnabled(isEditable);
	_distrib.setEditable(isEditable);

	_distribDirs.setList(descriptor.distrib.directories, resolver);
	_distribDirs.setEditable(isEditable);

	_applyButton.setEnabled(false);
	_discardButton.setEnabled(false);	  
	detectUpdates(true);
    }
    
    static private final Object NO_DISTRIB = new Object()
	{
	    public String toString()
	    {
		return "None selected";
	    }
	};
    static private final String DEFAULT_DISTRIB = "${application}.IcePatch2/server";

    private JTextField _name = new JTextField(20);
    private JTextArea _description = new JTextArea(3, 20);
    private MapField _variables;
    private JComboBox _distrib;
    private ListTextField _distribDirs = new ListTextField(20);
}