-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabView.java
More file actions
160 lines (144 loc) · 4.89 KB
/
Copy pathTabView.java
File metadata and controls
160 lines (144 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.awt.Component;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
public class TabView extends JPanel implements ActionListener {
private JTabbedPane tabbedPane = new JTabbedPane();
private ImageIcon icon = new ImageIcon("img/tab.gif");
private JPopupMenu contextMenu;
private JFileChooser fileChooser;
public TabView(){
this.add(tabbedPane);
setLayout(new GridLayout(1, 1));
contextMenu = new JPopupMenu();
init();
setupMenu();
}
public void init(){
tabbedPane.addMouseListener(new MouseAdapter () {
public void mousePressed(MouseEvent e) {
if(e.getModifiers()==InputEvent.BUTTON3_MASK){
contextMenu.show(e.getComponent(),
e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {}
});
fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save file");
fileChooser.setApproveButtonText("Save file");
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
}
public void addTab(String tabTitle, String info, StringBuffer code ){
//maybe switch on title to determine an image
tabbedPane.addTab(tabTitle, icon, new TextView(code, contextMenu), info);
tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
}
public void addHTMLTab(String tabTitle, String info, StringBuffer code, Node n ){
//maybe switch on title to determine an image
HTMLTextView html = new HTMLTextView(code, contextMenu, n);
tabbedPane.addTab(tabTitle, icon,html , info);
tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
}
public void addHTMLTab(String tabTitle, String info, StringBuffer code ){
//maybe switch on title to determine an image
HTMLTextView html = new HTMLTextView(code, contextMenu);
tabbedPane.addTab(tabTitle, icon,html , info);
tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
}
private void setupMenu() {
JMenuItem close = new JMenuItem("close");
JMenuItem closeall = new JMenuItem("close all");
JMenuItem save = new JMenuItem("save");
close.addActionListener(this);
closeall.addActionListener(this);
save.addActionListener(this);
contextMenu.add(close);
contextMenu.add(closeall);
contextMenu.add(save);
}
private void saveTab(String toSave, File f) {
boolean ok = false;
BufferedWriter out = null;
try {
ok = f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (ok) {
try {
out = new BufferedWriter(new FileWriter(f));
out.write(toSave);
} catch (IOException e1) {
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} else {
System.out.println("Couldnt create file...");
}
//}
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o instanceof JMenuItem){
JMenuItem sender = (JMenuItem)o;
String name = sender.getText();
if(name.equals("close")){
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}else if(name.equals("save")){
int index = tabbedPane.getSelectedIndex();
Component c = tabbedPane.getComponent(index);
Point p = c.getLocation();
Component t = tabbedPane.getComponentAt(new Point(p.x+20, p.y+30));
if (t instanceof TextView) {
TextView text = (TextView)t;
//String code = text.getText();
//save the code
//saveTab(code);
}
if (t instanceof HTMLTextView) {
HTMLTextView text = (HTMLTextView)t;
fileChooser.setSelectedFile(new File(text.node.getName()));
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
//save the code
String fileType = f.getName().substring(f.getName().lastIndexOf("."),f.getName().length());
if(fileType.indexOf("htm")!=-1){
//save as html
String code = text.getText();
saveTab(code,f);
}else if(fileType.indexOf("java")!=-1){
//save as java
String code = text.getUnModyfiedContent();
saveTab(code, f);
}else if(text.node.fileType.indexOf(FileTypes.MANIFEST)!=-1||text.node.fileType.indexOf(FileTypes.MANIFEST2)!=-1){
String code = text.getUnModyfiedContent();
saveTab(code, f);
System.out.println("Saved manifest");
}else{
System.out.println("FILETYPE UNKNOWN:" + text.node.fileType);
}
}
}
}else if(name.equals("close all")){
tabbedPane.removeAll();
}
}
}
}