summaryrefslogtreecommitdiff
path: root/java/src/IceGridGUI/LiveDeployment/WriteMessageDialog.java
blob: edb1588c4fda65973411bc394a98577f56670b6c (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
// **********************************************************************
//
// Copyright (c) 2003-2011 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.LiveDeployment;

import java.awt.Cursor;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.util.LayoutStyle;

import IceGrid.*;
import IceGridGUI.*;

class WriteMessageDialog extends JDialog
{
    WriteMessageDialog(final Root root)
    {
        super(root.getCoordinator().getMainFrame(), "Write Message - IceGrid Admin", true);
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        _mainFrame = root.getCoordinator().getMainFrame();

        _stdOut = new JRadioButton("Write to stdout");
        _stdOut.setSelected(true);
        JRadioButton stdErr = new JRadioButton("Write to stderr");
        ButtonGroup bg = new ButtonGroup();
        bg.add(_stdOut);
        bg.add(stdErr);

        JButton okButton = new JButton("OK");
        ActionListener okListener = new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    final Coordinator c = root.getCoordinator();

                    AdminPrx admin = c.getAdmin();
                    if(admin == null)
                    {
                        JOptionPane.showMessageDialog(
                            _mainFrame,
                            "No longer connected to IceGrid Registry",
                            "Writing message to server '" + _target + "' failed",
                            JOptionPane.ERROR_MESSAGE);
                    }
                    else
                    {

                        Ice.Identity adminId = new Ice.Identity(_target, c.getServerAdminCategory());

                        final Ice.ProcessPrx process = Ice.ProcessPrxHelper.uncheckedCast(
                            admin.ice_identity(adminId).ice_facet("Process"));

                        final String prefix = "Writing message to server '" + _target + "'...";
                        c.getStatusBar().setText(prefix);

                        Ice.AMI_Process_writeMessage cb = new Ice.AMI_Process_writeMessage()
                            {
                                public void ice_response()
                                {
                                    SwingUtilities.invokeLater(new Runnable()
                                        {
                                            public void run()
                                            {
                                                c.getStatusBar().setText(prefix + "done.");
                                            }
                                        });
                                }

                                public void ice_exception(final Ice.LocalException e)
                                {
                                    SwingUtilities.invokeLater(new Runnable()
                                        {
                                            public void run()
                                            {
                                                handleFailure("Communication exception: " + e.toString());
                                            }
                                        });
                                }

                                private void handleFailure(String message)
                                {
                                    c.getStatusBar().setText(prefix + "failed!");

                                    JOptionPane.showMessageDialog(
                                        _mainFrame,
                                        message,
                                        "Writing message to server '" + process.ice_getIdentity().name + "' failed",
                                        JOptionPane.ERROR_MESSAGE);
                                }
                            };

                        try
                        {
                            process.writeMessage_async(cb, _message.getText(), _stdOut.isSelected() ? 1 : 2);
                        }
                        catch(Ice.LocalException ex)
                        {
                            c.getStatusBar().setText(prefix + "failed.");
                            JOptionPane.showMessageDialog(
                                _mainFrame,
                                "Communication exception: " + ex.toString(),
                                "Writing message to server '" + _target + "' failed",
                                JOptionPane.ERROR_MESSAGE);

                            return;
                        }
                    }

                    setVisible(false);
                }
            };
        okButton.addActionListener(okListener);
        getRootPane().setDefaultButton(okButton);

        JButton cancelButton = new JButton("Cancel");
        ActionListener cancelListener = new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    setVisible(false);
                }
            };
        cancelButton.addActionListener(cancelListener);

        FormLayout layout = new FormLayout("left:pref, 3dlu, fill:pref:grow", "");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.setDefaultDialogBorder();
        builder.setRowGroupingEnabled(true);
        builder.setLineGapSize(LayoutStyle.getCurrent().getLinePad());

        _message.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(_message,
                                                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        builder.append(scrollPane, 3);
        builder.nextLine();
        builder.append(_stdOut);
        builder.append(stdErr);
        builder.nextLine();
        JComponent buttonBar = ButtonBarFactory.buildOKCancelBar(okButton, cancelButton);
        buttonBar.setBorder(Borders.DIALOG_BORDER);

        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.add(builder.getPanel());
        contentPane.add(buttonBar);

        pack();
        setResizable(false);
    }

    void showDialog(String serverId)
    {
        _target = serverId;
        _message.setText("");
        setLocationRelativeTo(_mainFrame);
        setVisible(true);
    }

    private JRadioButton _stdOut;
    private JTextArea _message = new JTextArea(3, 40);
    private String _target;
    private JFrame _mainFrame;
}