1 package org.ozoneDB.adminGui.main; 9 10 import org.ozoneDB.adminGui.feature.FeatureBar; 11 import org.ozoneDB.adminGui.feature.Feature; 12 import org.ozoneDB.adminGui.feature.FeaturePanel; 13 import org.ozoneDB.adminGui.feature.data.DataPanel; 14 import org.ozoneDB.adminGui.feature.server.ServerPanel; 15 import org.ozoneDB.adminGui.feature.account.AccountPanel; 16 import org.ozoneDB.adminGui.res.Settings; 17 import org.ozoneDB.adminGui.res.Images; 18 import org.ozoneDB.adminGui.res.OzoneTheme; 19 import org.ozoneDB.adminGui.widget.MessageBox; 20 import org.ozoneDB.ExternalDatabase; 21 import org.ozoneDB.core.admin.Admin; 22 23 import java.awt.*; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.WindowAdapter ; 26 import java.awt.event.WindowEvent ; 27 import java.util.ArrayList ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import javax.swing.*; 31 import javax.swing.plaf.metal.MetalLookAndFeel ; 32 33 36 public class AdminGui extends JFrame { 37 38 private JMenuBar menuBar; 39 private Action connectAction; 40 private Action disonnectAction; 41 42 public int ID_THRESHOLD = 99; 43 44 private JSplitPane centerSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 45 private HeaderPanel headerPanel = new HeaderPanel(""); 46 private FeatureBar featureBar; 47 private JPanel cardPanel; 48 private Feature feature; 49 private StatusBar statusBar; 50 private ExternalDatabase db; 51 private Set features; 52 53 private static AdminGui instance; 54 55 private AdminGui() { 56 super(); 57 initialize(); 58 } 59 60 public static AdminGui instance() { 61 if (instance == null) { 62 instance = new AdminGui(); 63 } 64 return instance; 65 } 66 67 public ExternalDatabase getDb() { 68 return db; 69 } 70 71 public Admin getAdmin() { 72 try { 73 return getDb().admin(); 74 } catch (Exception e) { 75 MessageBox.showError("Failed to get Admin from database: ", e.toString()); 76 return null; 77 } 78 } 79 80 private void initialize() { 81 this.features = new HashSet (); 82 setTitle(Settings.TITLE); 83 this.setSize(Settings.ADMIN_GUI_WIDTH, Settings.ADMIN_GUI_HEIGHT); 84 this.setIconImage(new ImageIcon(getClass().getResource(Images.IMAGE_LOGO)).getImage()); 85 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 86 this.addWindowListener(new WindowAdapter () { 87 public void windowClosing(WindowEvent e) { 88 exit(); 89 } 90 }); 91 92 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 93 94 layoutFrame(); 95 createMenus(); 96 setCursor(Cursor.getDefaultCursor()); 97 98 setExtendedState(JFrame.MAXIMIZED_BOTH); 99 setVisible(true); 100 } 101 102 public void setFeature(Feature feature) { 103 this.feature = feature; 104 headerPanel.setHeader(feature); 105 String featureName = feature.getName(); 106 if (!features.contains(featureName)) { 107 System.out.println("adding feature " + featureName + " to cards"); 108 features.add(feature.getName()); 109 FeaturePanel featurePanel = feature.getPanel(); 110 cardPanel.add(featurePanel, featureName); 111 featurePanel.activateFeature(); 112 } 113 CardLayout layout = (CardLayout) cardPanel.getLayout(); 114 System.out.println("showing feature " + featureName); 115 layout.show(cardPanel, featureName); 116 cardPanel.validate(); 117 } 118 119 public Feature getFeature() { 120 return this.feature; 121 } 122 123 143 private void layoutFrame() { 144 Container cp = getContentPane(); 145 146 statusBar = new StatusBar("Ozone Server Administration"); 147 148 featureBar = new FeatureBar(createFeatures()); 149 150 cardPanel = new JPanel(new CardLayout()); 151 cardPanel.add(new LogoPanel(), "logo"); 152 153 centerSplitPane.setTopComponent(headerPanel); 155 centerSplitPane.setBottomComponent(cardPanel); 156 centerSplitPane.setOneTouchExpandable(false); 157 centerSplitPane.setDividerSize(3); 159 centerSplitPane.setEnabled(false); 160 centerSplitPane.setBackground(Settings.COLOR_COBALT); 161 162 cp.add(featureBar, BorderLayout.WEST); 163 cp.add(centerSplitPane, BorderLayout.CENTER); 164 cp.add(statusBar, BorderLayout.SOUTH); 165 } 166 167 private void createMenus() { 168 menuBar = new JMenuBar(); 169 setJMenuBar(menuBar); 170 171 createFileMenu(); 172 } 173 174 private void createFileMenu() { 175 JMenu fileMenu = new JMenu("File"); 176 menuBar.add(fileMenu); 177 fileMenu.add(connectAction = new ConnectAction()); 178 fileMenu.add(disonnectAction = new DisconnectAction()); 179 fileMenu.add(new ExitAction()); 180 showDisconnected(); 181 } 182 183 private class ConnectAction extends AbstractAction { 184 public ConnectAction() { 185 super("Connect"); 186 } 187 188 public void actionPerformed(ActionEvent e) { 189 System.out.println("connect action called"); 190 connect(); 191 } 192 } 193 194 private void connect() { 195 ConnectionDialog connDialog = new ConnectionDialog(AdminGui.instance(), "Connect"); 196 connDialog.setVisible(true); 197 if (connDialog.isDbChanged()) { 198 db = connDialog.getDb(); 199 headerPanel.setConnected(connDialog.getUrl()); 200 disonnectAction.setEnabled(true); 201 connectAction.setEnabled(false); 202 featureBar.enableFeatures(); 203 } 204 } 205 206 private class DisconnectAction extends AbstractAction { 207 public DisconnectAction() { 208 super("Disconnect"); 209 } 210 211 public void actionPerformed(ActionEvent e) { 212 System.out.println("disconnect action called"); 213 try { 214 if (db != null && db.isOpen()) { 215 db.close(); 216 db = null; 217 showDisconnected(); 218 } 219 } catch (Exception e1) { 220 MessageBox.showError("Error closing db", e1.toString()); 221 e1.printStackTrace(); 222 } 223 } 224 } 225 226 public void showDisconnected() { 227 headerPanel.setDisConnected(); 228 disonnectAction.setEnabled(false); 229 connectAction.setEnabled(true); 230 featureBar.disableFeatures(); 231 } 232 233 private class ExitAction extends AbstractAction { 234 public ExitAction() { 235 super("Exit"); 236 } 237 238 public void actionPerformed(ActionEvent e) { 239 exit(); 240 } 241 } 242 243 private void exit() { 244 try { 245 if (db != null && db.isOpen()) { 246 db.close(); 247 } 248 } catch (Exception e1) { 249 e1.printStackTrace(); 250 } 251 System.exit(0); 252 } 253 254 private java.util.List createFeatures() { 255 java.util.List features = new ArrayList (); 256 features.add(new Feature(Settings.FEATURE_ACCOUNTS, Images.FEATURES_ACCOUNTS, AccountPanel.class)); 257 features.add(new Feature(Settings.FEATURE_SERVER, Images.FEATURES_SERVER, ServerPanel.class)); 258 features.add(new Feature(Settings.FEATURE_DATA, Images.FEATURES_DATA, DataPanel.class)); 259 return features; 260 } 261 262 private void run() { 263 System.out.println("Starting AdminGui ...."); 264 this.connect(); 265 } 266 267 public static void main(String args[]) { 268 MetalLookAndFeel.setCurrentTheme(new OzoneTheme()); 269 AdminGui gui = AdminGui.instance(); 270 gui.run(); 271 } 272 } | Popular Tags |