Skip to content

Commit 0df41f2

Browse files
author
mgricken
committed
Shuaib's features: New Class and Zoom for Print Preview.
Still needs some improvements, but I committed them in their original form, merged from r5004 into the latest trunk. M src/edu/rice/cs/drjava/model/AbstractGlobalModel.java M src/edu/rice/cs/drjava/config/OptionConstants.java M src/edu/rice/cs/drjava/ui/MainFrame.java AM src/edu/rice/cs/drjava/ui/NewJavaClassTest.java M src/edu/rice/cs/drjava/ui/PreviewFrame.java AM src/edu/rice/cs/drjava/ui/NewJavaClass.java M src/edu/rice/cs/util/FileOps.java git-svn-id: file:///tmp/test-svn/trunk@5174 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 8ffb3ce commit 0df41f2

File tree

7 files changed

+657
-12
lines changed

7 files changed

+657
-12
lines changed

drjava/src/edu/rice/cs/drjava/config/OptionConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ public static Vector<KeyStroke> vector(KeyStroke... ks) {
353353
new KeyStrokeOption("",null),
354354
to.vector(KeyStroke.getKeyStroke(KeyEvent.VK_N, MASK)));
355355

356+
/** The key binding for creating a new java class file */
357+
public static final VectorOption<KeyStroke> KEY_NEW_CLASS_FILE =
358+
new VectorOption<KeyStroke>("key.new.javafile", new KeyStrokeOption("",null), to.vector(KeyStroke.getKeyStroke(KeyEvent.VK_N, MASK|SHIFT_MASK)));
359+
356360
/** The key binding for opening an entire project. I is right next to O, so
357361
* it seemed logical that ctrl-I would open a project and ctrl-O open a file */
358362
public static final VectorOption<KeyStroke> KEY_OPEN_PROJECT =

drjava/src/edu/rice/cs/drjava/model/AbstractGlobalModel.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import edu.rice.cs.drjava.project.ProjectFileParserFacade;
119119
import edu.rice.cs.drjava.project.ProjectProfile;
120120
import edu.rice.cs.drjava.ui.DrJavaErrorHandler;
121+
import edu.rice.cs.drjava.ui.NewJavaClass;
121122

122123
import edu.rice.cs.plt.reflect.ReflectUtil;
123124
import edu.rice.cs.plt.tuple.Pair;
@@ -1161,6 +1162,26 @@ public OpenDefinitionsDocument newFile() {
11611162
return doc;
11621163
}
11631164

1165+
//newClass addition
1166+
1167+
public OpenDefinitionsDocument newClass(String methodName, String modifier, String className, boolean mainMethod, boolean classConstructor, String inheritance, String interfaces) {
1168+
1169+
NewJavaClass javaClass = new NewJavaClass();
1170+
String classContent = javaClass.createClassContent(methodName, modifier, className, mainMethod, classConstructor, inheritance, interfaces);
1171+
OpenDefinitionsDocument openDoc = newFile();
1172+
1173+
try {
1174+
openDoc.insertString(0, classContent, null);
1175+
openDoc.indentLines(0, classContent.length());
1176+
}
1177+
catch (BadLocationException ble) {
1178+
throw new UnexpectedException(ble);
1179+
}
1180+
1181+
return openDoc;
1182+
}
1183+
1184+
11641185
/** Creates a new junit test case.
11651186
* @param name the name of the new test case
11661187
* @param makeSetUp true iff an empty setUp() method should be included

drjava/src/edu/rice/cs/drjava/ui/MainFrame.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.AbstractMap;
5252
import java.util.ArrayList;
5353
import java.util.Collection;
54+
import java.util.Enumeration;
5455
import java.util.Set;
5556
import java.util.HashMap;
5657
import java.util.HashSet;
@@ -108,6 +109,7 @@
108109
import edu.rice.cs.util.swing.Utilities;
109110
import edu.rice.cs.util.swing.*;
110111

112+
import static edu.rice.cs.drjava.config.OptionConstants.KEY_NEW_CLASS_FILE;
111113
import static edu.rice.cs.drjava.ui.RecentFileManager.*;
112114
import static edu.rice.cs.drjava.ui.predictive.PredictiveInputModel.*;
113115
import static edu.rice.cs.util.XMLConfig.XMLConfigException;
@@ -426,6 +428,141 @@ public void actionPerformed(ActionEvent ae) {
426428
}
427429
};
428430

431+
432+
//newclass addition
433+
/** Creates a new Java class file. */
434+
private final Action _newClassAction = new AbstractAction("New Java Class") {
435+
public void actionPerformed(ActionEvent ae) {
436+
_newClassFileGUI();
437+
}
438+
};
439+
440+
//newclass addition
441+
public void _newClassFileGUI(){
442+
443+
final JFrame frame = new JFrame("New Java Class");
444+
final JButton createClass = new JButton("Create");
445+
final JTextField className = new JTextField(20);
446+
final JTextField interfaces = new JTextField(20);
447+
final JTextField superClass = new JTextField(20);
448+
final JLabel classNameLable = new JLabel("Class Name: ");
449+
final JLabel superClassLabel = new JLabel("SuperClass: ");
450+
final JLabel interfacesLabel = new JLabel("InterFaces: ");
451+
final JLabel modifierLabel = new JLabel("Modifier: ");
452+
final JLabel blankLabel = new JLabel(" ");
453+
454+
455+
final JLabel errorMessage = new JLabel();
456+
final JRadioButton defaultRadio= new JRadioButton("default", false);
457+
final JRadioButton publicRadio= new JRadioButton("public", true);
458+
final JRadioButton abstractRadio= new JRadioButton("abstract", false);
459+
final JRadioButton finalRadio= new JRadioButton("final", false);
460+
final ButtonGroup group1 = new ButtonGroup();
461+
final ButtonGroup group2 = new ButtonGroup();
462+
final JCheckBox mainMethod = new JCheckBox("Include main method");
463+
final JCheckBox classConstructor = new JCheckBox("Include class constructor");
464+
465+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
466+
frame.setSize(250, 350);
467+
frame.setLocationRelativeTo(null);
468+
frame.setVisible(true);
469+
470+
frame.setLayout(new FlowLayout());
471+
//the modifiers
472+
frame.add(modifierLabel);
473+
frame.add(publicRadio);
474+
frame.add(defaultRadio);
475+
476+
//blankLabel.setSize(150,2);
477+
frame.add(blankLabel);
478+
frame.add(abstractRadio);
479+
frame.add(finalRadio);
480+
481+
//grouping the modifiers
482+
group1.add(publicRadio);
483+
group1.add(defaultRadio);
484+
485+
group2.add(abstractRadio);
486+
group2.add(finalRadio);
487+
488+
frame.add(classNameLable);
489+
frame.add(className);
490+
frame.add(superClassLabel);
491+
frame.add(superClass);
492+
frame.add(interfacesLabel);
493+
frame.add(interfaces);
494+
495+
frame.add(errorMessage);
496+
frame.add(mainMethod);
497+
frame.add(classConstructor);
498+
frame.add(createClass);
499+
//listen to the button when it is pressed
500+
//createClass.addActionListener(this);
501+
502+
createClass.addActionListener(
503+
new ActionListener() {
504+
public void actionPerformed(ActionEvent e) {
505+
JRadioButton selectedRadioButton1 = new JRadioButton();
506+
JRadioButton selectedRadioButton2 = new JRadioButton();
507+
//to check which radio buttom has been choosen
508+
for (Enumeration enu = group1.getElements(); enu.hasMoreElements(); ) {
509+
JRadioButton b = (JRadioButton)enu.nextElement();
510+
if (b.getModel() == group1.getSelection())
511+
selectedRadioButton1 = b;
512+
}//for
513+
514+
for (Enumeration enu = group2.getElements(); enu.hasMoreElements(); ) {
515+
JRadioButton b = (JRadioButton)enu.nextElement();
516+
if (b.getModel() == group2.getSelection())
517+
selectedRadioButton2 = b;
518+
}//for
519+
520+
NewJavaClass javaClass = new NewJavaClass();
521+
boolean sc = false;
522+
boolean in = false;
523+
524+
// probability of entering the 3 variables
525+
String msg = "";
526+
527+
if(javaClass.classNameMeetsNamingConvention(className.getText())){
528+
529+
if(superClass.getText().length() != 0)
530+
if(javaClass.classNameMeetsNamingConvention(superClass.getText()))
531+
sc = true;
532+
else
533+
msg += "<html>Enter correct superclass name.<br></html>";
534+
535+
if(interfaces.getText().length() != 0)
536+
if(javaClass.interfacesNameMeetsNamingConvention(interfaces.getText()))
537+
in = true;
538+
else
539+
msg += "Enter correct interfaces name.";
540+
541+
if( ((superClass.getText().length() == 0 && interfaces.getText().length() == 0)) ||
542+
((superClass.getText().length() != 0 && sc == true) && (interfaces.getText().length() != 0 && in == true)) ||
543+
((superClass.getText().length() != 0 && sc == true) && (interfaces.getText().length() == 0)) ||
544+
((superClass.getText().length() == 0) && (interfaces.getText().length() != 0 && in == true))
545+
) {
546+
_model.newClass(selectedRadioButton1.getText(), selectedRadioButton2.getText() ,className.getText(), mainMethod.isSelected(), classConstructor.isSelected(), superClass.getText(), interfaces.getText());
547+
frame.setVisible(false);
548+
_save();
549+
} else {
550+
errorMessage.setForeground(Color.RED);
551+
errorMessage.setText(msg);
552+
}
553+
554+
} else {
555+
msg += "Enter correct class name. ";
556+
errorMessage.setForeground(Color.RED);
557+
errorMessage.setText(msg);
558+
}
559+
}
560+
}
561+
);
562+
}
563+
564+
565+
429566
private final Action _newProjectAction = new AbstractAction("New") {
430567
public void actionPerformed(ActionEvent ae) { _newProject(); }
431568
};
@@ -663,6 +800,15 @@ public void pack() {
663800
/** Supports MainFrameTest.*/
664801
public boolean isSaveEnabled() { return _saveAction.isEnabled(); }
665802

803+
//newclass addition
804+
/** Creates a new Java Class File in the current folder. */
805+
public final Action _newClassFileFolderAction = new AbstractAction("Create New Java Class in Folder") {
806+
public void actionPerformed(ActionEvent ae) {
807+
_newClassFileGUI();
808+
_findReplace.updateFirstDocInSearch();
809+
}
810+
};
811+
666812
/** Asks the user for a file name and saves the active document (in the definitions pane) to that file. */
667813
private final Action _saveAsAction = new AbstractAction("Save As...") {
668814
public void actionPerformed(ActionEvent ae) { _saveAs(); }
@@ -6145,6 +6291,7 @@ private void _removeErrorListener(OpenDefinitionsDocument doc) {
61456291
*/
61466292
private void _setUpActions() {
61476293
_setUpAction(_newAction, "New", "Create a new document");
6294+
_setUpAction(_newClassAction, "New", "Create a new Java Class");
61486295
_setUpAction(_newJUnitTestAction, "New", "Create a new JUnit test case class");
61496296
_setUpAction(_newProjectAction, "New", "Make a new project");
61506297
_setUpAction(_openAction, "Open", "Open an existing file");
@@ -6403,6 +6550,8 @@ private JMenu _setUpFileMenu(int mask) {
64036550
PlatformFactory.ONLY.setMnemonic(fileMenu,KeyEvent.VK_F);
64046551
// New, open
64056552
_addMenuItem(fileMenu, _newAction, KEY_NEW_FILE);
6553+
_addMenuItem(fileMenu, _newClassAction, KEY_NEW_CLASS_FILE);
6554+
64066555
_addMenuItem(fileMenu, _newJUnitTestAction, KEY_NEW_TEST);
64076556
_addMenuItem(fileMenu, _openAction, KEY_OPEN_FILE);
64086557
_addMenuItem(fileMenu, _openFolderAction, KEY_OPEN_FOLDER);
@@ -7510,6 +7659,7 @@ protected void _popupAction(MouseEvent e) {
75107659
// "New File in Folder" and "Open File in Folder" only work if exactly
75117660
// one folder is selected
75127661
m.add(_newFileFolderAction);
7662+
m.add(_newClassFileFolderAction);
75137663
m.add(_openOneFolderAction);
75147664

75157665
// get singular/plural right
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package edu.rice.cs.drjava.ui;
2+
3+
/*BEGIN_COPYRIGHT_BLOCK
4+
*
5+
* Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
16+
* names of its contributors may be used to endorse or promote products
17+
* derived from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*
31+
* This software is Open Source Initiative approved Open Source Software.
32+
* Open Source Initative Approved is a trademark of the Open Source Initiative.
33+
*
34+
* This file is part of DrJava. Download the current version of this project
35+
* from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
36+
*
37+
* END_COPYRIGHT_BLOCK*/
38+
39+
/** Generates Java source from information entered in the "New Class" dialog.
40+
* @version $Id$
41+
*/
42+
public class NewJavaClass {
43+
44+
//newclass addition
45+
public String capitalizeClassName(String name){
46+
return name.trim().substring(0,1).toUpperCase() + name.trim().substring(1);
47+
}
48+
49+
//newclass addition
50+
public boolean classNameMeetsNamingConvention(String name){
51+
if(name != null && name.length() != 0 && name.trim().matches("([A-Za-z_][A-Za-z0-9_]*)"))
52+
return true;
53+
else
54+
return false;
55+
}
56+
57+
public boolean iterateListOfClassNames(String name) {
58+
String[] separatedNames = name.split(",");
59+
Boolean correct = true;
60+
for(int i = 0; i < separatedNames.length; i++)
61+
if(!classNameMeetsNamingConvention(separatedNames[i].trim())) {
62+
correct = false;
63+
break;
64+
}
65+
return correct;
66+
}
67+
68+
public boolean interfacesNameMeetsNamingConvention(String name){
69+
if(name != null && name.length() != 0 && name.matches("([A-Za-z_][A-Za-z0-9_, ]*)")) {
70+
return iterateListOfClassNames(name);
71+
} else
72+
return false;
73+
}
74+
75+
public String capitalizeInterfacesNames(String name) {
76+
String[] separatedNames = name.split(",");
77+
String correctNames = "";
78+
for(int i = 0; i < separatedNames.length;) {
79+
correctNames += capitalizeClassName(separatedNames[i].trim());
80+
if(++i != separatedNames.length)
81+
correctNames += ", ";
82+
}
83+
return correctNames ;
84+
}
85+
86+
//newclass addition
87+
public String getMethodName(String methodName){
88+
if(methodName == null)
89+
return ""; //return blank method
90+
else if(methodName.equals("public") || methodName.equals("final") || methodName.equals("abstract"))
91+
return methodName + " ";
92+
else
93+
return ""; //return blank method
94+
}
95+
96+
//newclass addition
97+
public String createClassNameDecleration(String methodName, String modifier, String name,
98+
String superclass, String interfaces) {
99+
String declaration = "";
100+
if(name == null)
101+
declaration += getMethodName(methodName) + getMethodName(modifier) + "class"; //no class name returned because it is null
102+
else
103+
declaration += getMethodName(methodName) + getMethodName(modifier) + "class " + capitalizeClassName(name);
104+
if(superclass.length() != 0) {
105+
declaration += " extends " + capitalizeClassName(superclass);
106+
}
107+
if(interfaces.length() != 0) {
108+
declaration += " implements " + capitalizeInterfacesNames(interfaces);
109+
}
110+
return declaration;
111+
}
112+
113+
public String createClassContent(String methodName, String modifier, String className,
114+
boolean mainMethod, boolean classConstructor, String inheritance,
115+
String interfaces){
116+
String classContent = "";
117+
118+
classContent += "/**\n";
119+
classContent += "* Auto Generated Java Class.\n";
120+
classContent += "*/\n";
121+
classContent += createClassNameDecleration(methodName, modifier, className, inheritance, interfaces);
122+
classContent += " {\n";
123+
classContent += "\n";
124+
125+
if(classConstructor) {
126+
classContent += "public " + capitalizeClassName(className) + "() { \n";
127+
// classContent += getMethodName(methodName) + " " + capitalizeClassName(className) + "() { \n";
128+
classContent += "/* YOUR CONSTRUCTOR CODE HERE*/";
129+
classContent += "\n}\n";
130+
}
131+
132+
if(mainMethod) {
133+
classContent += "\n public static void main(String [] args) { \n\n";
134+
classContent += "}\n\n";
135+
}
136+
137+
classContent += "/* ADD YOUR CODE HERE */\n";
138+
classContent += "\n";
139+
classContent += "}\n";
140+
return classContent;
141+
}
142+
}

0 commit comments

Comments
 (0)