View Javadoc
1 /* 2 * 2003 (c) All rights reserved. 3 */ 4 package portaview; 5 6 import java.awt.BorderLayout; 7 import java.awt.Component; 8 import java.awt.Dimension; 9 import java.awt.event.WindowAdapter; 10 import java.awt.event.WindowEvent; 11 import java.io.File; 12 import java.net.InetAddress; 13 import java.util.List; 14 import java.util.Vector; 15 16 import javax.swing.BorderFactory; 17 import javax.swing.JFrame; 18 import javax.swing.JLabel; 19 import javax.swing.JOptionPane; 20 import javax.swing.JPanel; 21 import javax.swing.JScrollPane; 22 import javax.swing.JSplitPane; 23 import javax.swing.JTextArea; 24 import javax.swing.SwingConstants; 25 26 import org.apache.commons.cli.CommandLine; 27 import org.apache.commons.cli.CommandLineParser; 28 import org.apache.commons.cli.Options; 29 import org.apache.commons.cli.ParseException; 30 import org.apache.commons.cli.PosixParser; 31 32 import portaview.model.AlbumModel; 33 import portaview.model.PortaViewCollection; 34 import portaview.model.PortaViewModel; 35 import portaview.server.PortaViewClient; 36 import portaview.server.PortaViewServer; 37 import portaview.server.Repository; 38 import portaview.view.AlbumPanel; 39 import portaview.view.PortaViewPanel; 40 41 /*** 42 * This is the main PortaView class. Essentially it contains two things. 43 * The PortaView Manager and PortaView displayer. It also initializes the 44 * necessary XML-RPC server so remote host can talk to this instance. 45 * 46 * @author William Lee 47 * 48 */ 49 public class PortaViewMain extends DefaultFrame 50 { 51 private static int WIDTH = 1024; 52 private static int HEIGHT = 700; 53 54 private static JTextArea _debugPanel = new JTextArea(70, 1000); 55 private Displayer displayer = null; 56 private Previewer preview = null; 57 private PortaViewModel thisPortaViewModel = null; 58 private PortaViewPanel portaViewPanel = null; 59 private AlbumPanel albumPanel = null; 60 private List albums = null; 61 /*** 62 * Indicates whether the display is active. 63 */ 64 private boolean displayActive = false; 65 66 /*** 67 * Constructs the main PortaView application, given the command-line arguments. 68 * @param args command-line arguments. 69 * @throws Exception if the initialization routine goes wrong. 70 */ 71 public PortaViewMain(String[] args) throws Exception 72 { 73 super(); 74 initRegistry(args); 75 albums = new Vector(); 76 thisPortaViewModel = 77 new PortaViewModel( 78 Registry.getServer().getName(), 79 Registry.getServer().getInetAddress(), 80 AlbumModel.getEmptyAlbumModel()); 81 initDisplayer(); 82 setTitle( 83 "PortaView Manager: Current PortaView: (" 84 + Registry.getServer().getName() 85 + ")"); 86 initManager(); 87 } 88 89 /*** 90 * Initializes the displayer. 91 * 92 */ 93 public void initDisplayer() 94 { 95 displayer = new Displayer(getThisPortaViewModel()); 96 } 97 98 /*** 99 * Initializes the manager. 100 */ 101 private void initManager() 102 { 103 setSize(WIDTH, HEIGHT); 104 105 // Generate the PortaViewPanel 106 JPanel pvpanel = new JPanel(new BorderLayout()); 107 pvpanel.add( 108 WidgetFactory.createCenteredLabel("PortaViews"), 109 BorderLayout.NORTH); 110 /* 111 pvpanel.add( 112 new JLabel("( * ) You are at this PortaView"), 113 BorderLayout.SOUTH); 114 */ 115 portaViewPanel = 116 new PortaViewPanel(Registry.getServer().getRegisteredPortaViews()); 117 portaViewPanel.setPreferredSize(new Dimension(150, 500)); 118 pvpanel.add(portaViewPanel, BorderLayout.CENTER); 119 120 // Generate the AlbumPanel 121 JPanel apanel = new JPanel(); 122 apanel.setLayout(new BorderLayout()); 123 JLabel albl = new JLabel("Albums"); 124 albl.setHorizontalAlignment(SwingConstants.CENTER); 125 albl.setVerticalAlignment(SwingConstants.CENTER); 126 albl.setBorder(BorderFactory.createEtchedBorder()); 127 apanel.add(albl, BorderLayout.NORTH); 128 albumPanel = new AlbumPanel(Registry.getServer().getAlbums()); 129 //albumPanel.setPreferredSize(new Dimension(150, 500)); 130 apanel.add(albumPanel, BorderLayout.CENTER); 131 132 // this is experimental implementation of the display panel. 133 PortaViewModel pvm = getThisPortaViewModel(); 134 // Initialize the displayer controller 135 preview = new Previewer(pvm); 136 137 JSplitPane rightPane = 138 new JSplitPane( 139 JSplitPane.VERTICAL_SPLIT, 140 preview.getView(), 141 apanel); 142 143 JSplitPane mainPane = 144 new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pvpanel, rightPane); 145 getContentPane().add(mainPane); 146 } 147 148 /*** 149 * Prints out a debug message. 150 * @param str message to print 151 */ 152 public static void debug(String str) 153 { 154 System.out.println(str); 155 _debugPanel.setText(_debugPanel.getText() + str + "\n"); 156 } 157 158 /*** 159 * Constructs a debug panel. 160 */ 161 public static void configureDebugPanel() 162 { 163 _debugPanel.setPreferredSize(new Dimension(300, 50)); 164 } 165 166 /*** 167 * Sets up the PortaView server based on the command line argument 168 * @param cmd the command line arguments. 169 */ 170 private void setupServer(CommandLine cmd) throws Exception 171 { 172 boolean isMaster = cmd.hasOption("m"); 173 String name = "(No Name)"; 174 String masterIP = "127.0.0.1"; 175 String pvIP = "127.0.0.1"; 176 String repo = "albums"; 177 int port = 8888; 178 179 if (cmd.hasOption("n")) 180 { 181 name = cmd.getOptionValue("n"); 182 } 183 if (cmd.hasOption("c")) 184 { 185 masterIP = cmd.getOptionValue("c"); 186 } 187 if (cmd.hasOption("i")) 188 { 189 pvIP = cmd.getOptionValue("i"); 190 } 191 if (cmd.hasOption("a")) 192 { 193 repo = cmd.getOptionValue("a"); 194 } 195 // Creates the Server 196 Registry.setServer( 197 new PortaViewServer( 198 name, 199 InetAddress.getByName(pvIP), 200 InetAddress.getByName(masterIP), 201 new Repository(new File(repo)), 202 Consts.XML_RPC_PORT)); 203 Registry.setClient(new PortaViewClient()); 204 } 205 206 /*** 207 * Parses the command line argument. 208 * @param args arguments passed from the command line arguments. 209 */ 210 private void initRegistry(String[] args) throws Exception 211 { 212 CommandLineParser parser = new PosixParser(); 213 Options options = new Options(); 214 CommandLine cmd = null; 215 Registry.reset(); 216 // parses the command line 217 options.addOption("m", "master", false, "set this as master device."); 218 options.addOption("n", "name", true, "name for the PortaView."); 219 options.addOption("i", "ip", true, "this PortaView's address."); 220 options.addOption( 221 "c", 222 "connect", 223 true, 224 "connect to address (master ip address)."); 225 options.addOption( 226 "a", 227 "album-dir", 228 true, 229 "the album directory (only if this PortaView is a master)."); 230 try 231 { 232 cmd = parser.parse(options, args); 233 setupServer(cmd); 234 // get whether this is a master 235 Registry.setMaster(cmd.hasOption("m")); 236 // set the main instance to the registry. 237 Registry.setMainApplication(this); 238 } 239 catch (ParseException exp) 240 { 241 System.err.println("Illegal argument. Reason: " + exp.getMessage()); 242 throw exp; 243 } 244 catch (Exception e) 245 { 246 System.err.println( 247 "May have illegal address or unknown problem. Reason: " 248 + e.toString()); 249 throw e; 250 } 251 } 252 253 /*** 254 * Starting point for PortaView's main execution. 255 * @param args command-line arguments 256 */ 257 public static void main(String[] args) 258 { 259 String[] gargs = getGUIArgs(args); 260 PortaViewMain pv = null; 261 try 262 { 263 pv = new PortaViewMain(gargs); 264 pv.start(); 265 pv.setVisible(true); 266 } 267 catch (Exception e) 268 { 269 if (pv != null) 270 pv.shutdown(); 271 System.err.println(e.toString()); 272 e.printStackTrace(); 273 System.exit(1); 274 } 275 } 276 277 /*** 278 * If the user does not type in a command line argument, pop up a box 279 * for them to type it in. 280 * @return the new command line argument after prompting the user 281 * if args does not contain anything. 282 */ 283 public static String[] getGUIArgs(String[] args) 284 { 285 if (args.length == 0) 286 { 287 // Ask whether this is a master PortaView 288 boolean isMaster = true; 289 int answer = 290 JOptionPane.showConfirmDialog( 291 null, 292 "Is this a Master PortaView?\n" 293 + "(If this PortaView is a Slave PortaView, select 'No'", 294 "Master PortaView?", 295 JOptionPane.YES_NO_OPTION); 296 if (answer == JOptionPane.NO_OPTION) 297 { 298 isMaster = false; 299 } 300 String name = "Untitled"; 301 name = JOptionPane.showInputDialog("Name of this PortaView?", name); 302 String ip = "127.0.0.1"; 303 ip = 304 JOptionPane.showInputDialog( 305 "PortaView Internet Protocol Address:\n" 306 + "This address should be an IP address that is\n" 307 + "visible to external network.", 308 ip); 309 String masterIp = ip; 310 if (!isMaster) 311 { 312 masterIp = 313 JOptionPane.showInputDialog( 314 "Master PortaView IP Address", 315 "127.0.0.1"); 316 } 317 318 String repo = "src/test/test/portaview/images"; 319 if (isMaster) 320 repo = JOptionPane.showInputDialog("Album directory?", repo); 321 if (isMaster) 322 { 323 String[] rtnMaster = 324 { "-m", "-n", name, "-i", ip, "-c", masterIp, "-a", repo }; 325 return rtnMaster; 326 } 327 else 328 { 329 330 String[] rtnSlave = { "-n", name, "-i", ip, "-c", masterIp }; 331 return rtnSlave; 332 } 333 334 } 335 return args; 336 } 337 338 /*** 339 * Populate the frame with the main panel. 340 * @param panel 341 */ 342 private static void populateFrame(JFrame frame, Component c) 343 { 344 frame.getContentPane().setLayout(new BorderLayout()); 345 frame.getContentPane().add(c, BorderLayout.CENTER); 346 JScrollPane scroll = new JScrollPane(_debugPanel); 347 scroll.setPreferredSize(new Dimension(300, 100)); 348 scroll.setHorizontalScrollBarPolicy( 349 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 350 frame.getContentPane().add(scroll, BorderLayout.SOUTH); 351 } 352 353 /*** 354 * Starts the main application. 355 */ 356 public void start() throws Exception 357 { 358 Registry.getServer().start(); 359 PortaViewCollection pvc = 360 Registry.getServer().getRegisteredPortaViews(); 361 pvc.add(thisPortaViewModel); 362 // force the views that based on this portview model to refresh 363 thisPortaViewModel.forceNotifyObservers(); 364 //select the first PortaView 365 portaViewPanel.selectFirst(); 366 if (!Registry.isMaster()) 367 { 368 Registry.getClient().registerWithMaster(); 369 } 370 371 // Initailize the preview controller 372 PortaViewModel pvm = getThisPortaViewModel(); 373 // populate the album panel. It'll take a long time. 374 Registry.getServer().setAlbums( 375 Registry.getClient().getAlbumsFromMaster()); 376 albumPanel.refresh(); 377 } 378 379 /*** 380 * Closes all the resources that this PortaView opens. 381 */ 382 public void shutdown() 383 { 384 // If this is a slave server, then we unregister with the master 385 if (!Registry.isMaster()) 386 Registry.getClient().unregisterWithMaster(); 387 Registry.getServer().shutdown(); 388 } 389 390 /*** 391 * The listener that handles the windows exit event. It'll try to shutdown the 392 * server and exit. 393 * 394 * @return a window listener that just call System.exist(0); 395 */ 396 protected WindowAdapter getDefaultWindowAdapter() 397 { 398 WindowAdapter rtn = (new WindowAdapter() 399 { 400 public void windowClosing(WindowEvent e) 401 { 402 Log.debug("Shutting down the server."); 403 shutdown(); 404 System.exit(0); 405 } 406 407 public void windowDeiconified(WindowEvent e) 408 { 409 } 410 411 public void windowIconified(WindowEvent e) 412 { 413 } 414 }); 415 return rtn; 416 } 417 418 /*** 419 * @return 420 */ 421 public Displayer getDisplayer() 422 { 423 return displayer; 424 } 425 426 /*** 427 * @return 428 */ 429 public Previewer getPreview() 430 { 431 return preview; 432 } 433 434 /*** 435 * @param displayer 436 */ 437 public void setDisplayer(Displayer displayer) 438 { 439 this.displayer = displayer; 440 } 441 442 /*** 443 * @param previewer 444 */ 445 public void setPreview(Previewer previewer) 446 { 447 preview = previewer; 448 } 449 450 /*** 451 * @return 452 */ 453 public PortaViewModel getThisPortaViewModel() 454 { 455 return thisPortaViewModel; 456 } 457 458 /*** 459 * @param model 460 */ 461 public void setThisPortaViewModel(PortaViewModel model) 462 { 463 thisPortaViewModel = model; 464 } 465 466 /*** 467 * @return 468 */ 469 public boolean isDisplayActive() 470 { 471 return displayActive; 472 } 473 474 /*** 475 * @param b 476 */ 477 public void setDisplayActive(boolean b) 478 { 479 displayActive = b; 480 // if deactive the display, we need to refresh the preview panel and 481 // all the portaviews icons. 482 if (!b) 483 { 484 getPreview().getView().refresh(); 485 portaViewPanel.refresh(); 486 } 487 } 488 489 }

This page was automatically generated by Maven