001 /* 002 * CRIMSON 003 * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 004 * University of Pennsylvania. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License as 008 * published by the Free Software Foundation; either version 2 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, but 012 * WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 * 02110-1301 USA. 020 * 021 * @(#)ModelEditor.java 022 */ 023 024 package edu.upenn.crimson.gui; 025 026 import edu.upenn.crimson.*; 027 import java.awt.*; 028 import java.awt.event.*; 029 import javax.swing.*; 030 import javax.swing.border.Border; 031 import javax.swing.text.*; 032 033 /** 034 * This is a graphical model editor. 035 * 036 * @author Stephen Fisher 037 * @version $Id: ModelEditor.java,v 1.6 2007/05/16 18:55:58 fisher Exp $ 038 */ 039 public class ModelEditor extends JFrame { 040 private ModelEditor thisFrame; 041 042 private Model model = null; 043 044 JTextArea notesTA; 045 JTextArea detailsTA; 046 047 public ModelEditor(Model modelObj) { 048 super("Model Editor"); 049 050 // keep pointer to self so can 'dispose' Frame below 051 thisFrame = this; 052 053 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 054 055 if (modelObj == null) { 056 CrimsonUtils.printMsg("Can not edit null model.", CrimsonUtils.ERROR); 057 thisFrame.dispose(); 058 return; 059 } 060 this.model = modelObj; 061 062 Border emptyBorder = BorderFactory.createEmptyBorder(5,5,5,5); 063 Border loweredBorder = BorderFactory.createLoweredBevelBorder(); 064 Border etchedBorder = BorderFactory.createEtchedBorder(); 065 Border etchedPadBorder5 = 066 BorderFactory.createCompoundBorder(etchedBorder, emptyBorder); 067 Border loweredPadBorder5 = 068 BorderFactory.createCompoundBorder(loweredBorder, emptyBorder); 069 070 // ************************************************* 071 // SETUP BUTTONS 072 JPanel buttonP = new JPanel(new BorderLayout()); 073 JButton saveB = new JButton(new ImageIcon("icons/save.png")); 074 saveB.setToolTipText("Save changes"); 075 saveB.addActionListener(new ActionListener() { 076 public void actionPerformed(ActionEvent e) { 077 if (model == null) { 078 return; 079 } else { 080 // save the notes without updating the 081 // database, because this is done when we save 082 // the details 083 model.setNotes(notesTA.getText(), false); 084 model.setDetails(detailsTA.getText(), true); 085 } 086 087 // make sure the ModelManager reflects the changes 088 ModelManager.redraw(); 089 090 thisFrame.dispose(); 091 } 092 }); 093 buttonP.add(saveB, BorderLayout.WEST); 094 JButton cancelB = new JButton(new ImageIcon("icons/close.png")); 095 cancelB.setToolTipText("Cancel"); 096 cancelB.addActionListener(new ActionListener() { 097 public void actionPerformed(ActionEvent e) { 098 thisFrame.dispose(); 099 } 100 }); 101 buttonP.add(cancelB, BorderLayout.EAST); 102 103 // ************************************************* 104 // SETUP NOTES TEXTAREA 105 JPanel notesP = new JPanel(new BorderLayout()); 106 notesP.setBorder(etchedPadBorder5); 107 notesP.add(new JLabel("Notes (4000 char limit): "), BorderLayout.NORTH); 108 109 notesTA = new JTextArea(); 110 notesTA.setLineWrap(true); 111 notesTA.setWrapStyleWord(true); 112 PlainDocument notesDoc = new PlainDocument(); 113 notesDoc.setDocumentFilter(GUIUtils.getDocLengthFilter()); 114 notesTA.setDocument(notesDoc); 115 notesTA.setText(model.getNotes()); 116 JScrollPane notesSP = new JScrollPane(notesTA); 117 notesSP.setBorder(loweredPadBorder5); 118 notesSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 119 notesSP.setPreferredSize(new Dimension(400, 200)); 120 notesP.add(notesSP, BorderLayout.CENTER); 121 122 // ************************************************* 123 // SETUP DETAILS TEXTAREA 124 JPanel detailsP = new JPanel(new BorderLayout()); 125 detailsP.setBorder(etchedPadBorder5); 126 detailsP.add(new JLabel("Details: "), BorderLayout.NORTH); 127 128 detailsTA = new JTextArea(model.getDetails()); 129 detailsTA.setLineWrap(true); 130 detailsTA.setWrapStyleWord(true); 131 JScrollPane detailsSP = new JScrollPane(detailsTA); 132 detailsSP.setBorder(loweredPadBorder5); 133 detailsSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 134 detailsSP.setPreferredSize(new Dimension(400, 200)); 135 detailsP.add(detailsSP, BorderLayout.CENTER); 136 137 // ************************************************* 138 // SETUP PANELS 139 JPanel infoP = new JPanel(new BorderLayout()); 140 infoP.setBorder(etchedPadBorder5); 141 infoP.add(new JLabel("Model: " + model.getID()), BorderLayout.WEST); 142 143 JPanel valuesP = new JPanel(new BorderLayout()); 144 valuesP.setBorder(etchedPadBorder5); 145 valuesP.add(notesP, BorderLayout.NORTH); 146 valuesP.add(detailsP, BorderLayout.CENTER); 147 148 getContentPane().setLayout(new BorderLayout()); 149 getContentPane().add(infoP, BorderLayout.NORTH); 150 getContentPane().add(valuesP, BorderLayout.CENTER); 151 getContentPane().add(buttonP, BorderLayout.SOUTH); 152 pack(); 153 154 // set the default window size 155 setSize(getSize().width + 75, getSize().height + 30); 156 157 // display the window 158 setVisible(true); 159 } 160 161 } // ModelEditor.java 162