001/*
002 * Copyright (c) 2010-2011 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-05-09 11:05:40 -0700 (Wed, 09 May 2012) $' 
007 * '$Revision: 29823 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui.memory;
031
032import java.awt.GridBagLayout;
033import java.awt.GridLayout;
034import java.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036import java.awt.event.KeyEvent;
037import java.awt.event.WindowAdapter;
038import java.awt.event.WindowEvent;
039
040import javax.swing.JButton;
041import javax.swing.JComponent;
042import javax.swing.JDialog;
043import javax.swing.JLabel;
044import javax.swing.JPanel;
045import javax.swing.JTextField;
046import javax.swing.KeyStroke;
047
048import org.kepler.build.project.MemoryProperties;
049
050/**
051 * Created by David Welker.
052 * Date: 12/21/10
053 * Time: 5:35 PM
054 */
055public class JvmMemoryPanel extends JPanel
056{
057    private JTextField minMemoryField;
058    private JTextField maxMemoryField;
059    private JTextField stackSizeField;
060    private JButton buttonOK;
061    private JButton buttonCancel;
062    private JDialog owner;
063
064    public JvmMemoryPanel(JDialog owner)
065    {
066        super(new GridBagLayout());
067        this.owner = owner;
068        owner.setTitle("Kepler JVM Memory Settings");
069        owner.setModal(true);
070        layoutComponents();
071        owner.getRootPane().setDefaultButton(buttonOK);
072
073        owner.addWindowFocusListener(new WindowAdapter()
074        {
075            @Override
076            public void windowGainedFocus(WindowEvent e)
077            {
078                buttonOK.requestFocusInWindow();
079            }
080        });
081
082
083        buttonOK.addActionListener(new ActionListener()
084        {
085            @Override
086            public void actionPerformed(ActionEvent e)
087            {
088                onOK();
089            }
090        });
091
092        buttonCancel.addActionListener(new ActionListener()
093        {
094            @Override
095            public void actionPerformed(ActionEvent e)
096            {
097                onCancel();
098            }
099        });
100
101
102        owner.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
103        owner.addWindowListener(new WindowAdapter()
104        {
105            @Override
106            public void windowClosing(WindowEvent e)
107            {
108                onCancel();
109            }
110        });
111
112        registerKeyboardAction(new ActionListener()
113        {
114            @Override
115            public void actionPerformed(ActionEvent e)
116            {
117                onCancel();
118            }
119        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
120
121    }
122
123    private void onOK()
124    {
125        MemoryProperties.setMinMemory(minMemoryField.getText());
126        MemoryProperties.setMaxMemory(maxMemoryField.getText());
127        MemoryProperties.setStackSize(stackSizeField.getText());
128        owner.dispose();
129    }
130
131    private void onCancel()
132    {
133        owner.dispose();
134    }
135
136    private void layoutComponents()
137    {
138        setLayout(new GridLayout(0, 2));
139
140        JLabel minMemoryLabel = new JLabel("Min Memory (-Xms):");
141        add(minMemoryLabel);
142        minMemoryField = new JTextField(MemoryProperties.getMinMemory());
143        add(minMemoryField);
144        
145        JLabel maxMemoryLabel = new JLabel("Max Memory (-Xmx):");
146        add(maxMemoryLabel);
147        maxMemoryField = new JTextField(MemoryProperties.getMaxMemory());
148        add(maxMemoryField);
149        
150        JLabel stackSizeLabel = new JLabel("Stack Size (-Xss):");
151        add(stackSizeLabel);
152        stackSizeField = new JTextField(MemoryProperties.getStackSize());
153        add(stackSizeField);
154                
155        JLabel note = new JLabel("<html>Note: You must restart Kepler<br/>" +
156                "for memory changes to take effect.</html>");
157        add(note);
158        
159        JPanel okCancel = new JPanel();
160        buttonOK = new JButton("OK");
161        buttonCancel = new JButton("Cancel");
162        okCancel.add(buttonOK);
163        okCancel.add(buttonCancel);
164        add(okCancel);
165    }
166
167    public static void main(String[] args)
168    {
169        JDialog dialog = new JDialog();
170        dialog.setContentPane(new JvmMemoryPanel(dialog));
171        dialog.pack();
172        dialog.setVisible(true);
173        //System.exit(0);
174    }
175
176
177}