-
Notifications
You must be signed in to change notification settings - Fork 3k
/
MainPanel.java
237 lines (194 loc) · 8.47 KB
/
MainPanel.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.androidstudio.motionlayoutcycles;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicArrowButton;
/**
* This provide the overall frame and top level ui elements
*/
public class MainPanel extends JFrame {
JTextArea myXmlOutput = new JTextArea();
JScrollPane myXmlScrollPane = new JScrollPane(myXmlOutput);
JButton myPlayButton = new JButton("Play");
JComboBox<String> baseMovement = new JComboBox<>(AnimationPanel.MOVE_NAMES);
JComboBox<String> duration = new JComboBox<>(AnimationPanel.DURATION);
JComboBox<String> easing = new JComboBox<>(AnimationPanel.EASING_OPTIONS);
JPanel main = new JPanel(new BorderLayout(5,5));
JPanel main1 = new JPanel(new BorderLayout(5,5));
JPanel main2 = new JPanel(new BorderLayout(5,5));
GridLayout myGraphLayout = new GridLayout(1, 1);
JPanel myGraphs = new JPanel(myGraphLayout);
JMenuBar topMenu = new JMenuBar();
JPanel base = new JPanel(new BorderLayout());
JTabbedPane myCycleEditTabs = new JTabbedPane();
CycleSetModel myCycleSetModel = new CycleSetModel(myXmlOutput);
private CycleSetModel.Cycle createCycle() {
return myCycleSetModel.createCycle();
}
AnimationPanel animationPanel = new AnimationPanel(myCycleSetModel, myPlayButton);
public static JButton createTabbButton(String text) {
JButton ret = new JButton(text);
ret.setBorder(null);
if (text == null) {
ret.setIcon(UIManager.getIcon("InternalFrame.paletteCloseIcon"));
}
ret.setFocusPainted(false);
ret.setContentAreaFilled(false);
ret.setBorderPainted(true);
ret.setBackground(null);
ret.setHorizontalTextPosition(SwingConstants.LEFT);
ret.setMargin(new Insets(0, 0, 0, 0));
return ret;
}
MainPanel() {
super("Cycle Editor");
setBounds(new Rectangle(1000, 900));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myCycleSetModel.myMainPanel = this;
CycleSetModel.Cycle myCycle;
CycleEdit cycleEdit;
myCycle = createCycle();
myGraphs.add(myCycle.myView);
main.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
main.add(main1,BorderLayout.CENTER);
main.add(main2,BorderLayout.SOUTH);
main2.add(animationPanel,BorderLayout.CENTER);
main1.add(myGraphs,BorderLayout.CENTER);
main2.add(myXmlScrollPane,BorderLayout.EAST);
main1.add(base,BorderLayout.EAST);
myXmlScrollPane.setPreferredSize(new Dimension(100, 300));
BasicArrowButton next = new BasicArrowButton(BasicArrowButton.EAST);
BasicArrowButton prev = new BasicArrowButton(BasicArrowButton.WEST);
myCycle.myModel.delete = next;
// add the first panel
cycleEdit = new CycleEdit(myCycle.myView, myCycle.myModel, animationPanel);
myCycle.myControl = cycleEdit;
JScrollPane scrollPane = new JScrollPane(cycleEdit);
myCycleEditTabs.add(scrollPane);
cycleEdit.updateTabName(myCycle.myModel.getAttName());
// add the add more panel
myCycleEditTabs.add(new JPanel(), "+");
JButton newTabbButton = createTabbButton("+");
newTabbButton.addActionListener(e -> createNewCycle());
myCycleEditTabs.setTabComponentAt(1, newTabbButton);
base.add(myCycleEditTabs);
JPanel bottomControls = new JPanel();
base.add(bottomControls, BorderLayout.SOUTH);
myPlayButton.setText("XXXXX");
myPlayButton.setPreferredSize(myPlayButton.getPreferredSize());
myPlayButton.setText("Play");
bottomControls.add(myPlayButton);
baseMovement
.addActionListener((e) -> animationPanel.setMovement(baseMovement.getSelectedIndex()));
bottomControls.add(baseMovement);
duration.setSelectedIndex(AnimationPanel.DURATION.length - 1);
duration.addActionListener((e) -> animationPanel.setDurationIndex(duration.getSelectedIndex()));
bottomControls.add(duration);
easing.addActionListener((e) -> animationPanel.setEasing((String) easing.getSelectedItem()));
bottomControls.add(easing);
myXmlScrollPane.setPreferredSize(base.getPreferredSize());
setContentPane(main);
validate();
myCycle.myModel.update();
JMenu menu = new JMenu("File");
topMenu.add(menu);
JMenuItem menuItem = new JMenuItem("parse xml", KeyEvent.VK_T);
menuItem.addActionListener(e -> myCycle.myModelSet.parse());
menu.add(menuItem);
menu = new JMenu("Examples");
topMenu.add(menu);
for (int i = 0; i < KeyCycleExamples.all.length; i++) {
String text = KeyCycleExamples.all[i][1];
int speed = Integer.parseInt(KeyCycleExamples.all[i][2]);
int movement = Integer.parseInt(KeyCycleExamples.all[i][3]);
int easingType = Integer.parseInt(KeyCycleExamples.all[i][4]);
menuItem = new JMenuItem(KeyCycleExamples.all[i][0]);
menuItem.addActionListener(e -> {
myXmlOutput.setText(text);
duration.setSelectedIndex(speed);
baseMovement.setSelectedIndex(movement);
easing.setSelectedIndex(easingType);
myCycle.myModelSet.parse();
animationPanel.play();
});
menu.add(menuItem);
}
menu = new JMenu("Cycle");
menuItem = new JMenuItem("Add cycle", KeyEvent.VK_A);
menuItem.addActionListener(e -> createNewCycle());
menu.add(menuItem);
menuItem = new JMenuItem("Remove cycle", KeyEvent.VK_R);
menuItem.addActionListener(e -> removeCurrentCycle());
menu.add(menuItem);
topMenu.add(menu);
menuItem = new JMenuItem("Play", KeyEvent.VK_P);
menuItem.addActionListener(e -> animationPanel.play());
topMenu.add(menuItem);
this.setJMenuBar(topMenu);
}
public CycleSetModel.Cycle createNewCycle() {
CycleSetModel.Cycle cycle = createCycle();
CycleEdit cycleEdit = new CycleEdit(cycle.myView, cycle.myModel, animationPanel);
cycle.myControl = cycleEdit;
cycleEdit.setRemoveCallback(e -> removeCycle(cycle));
int count = myCycleEditTabs.getTabCount();
myCycleEditTabs.insertTab("label", null, cycleEdit, "tooltip", count - 1);
cycleEdit.updateTabName(cycle.myModel.getAttName());
myGraphs.add(cycle.myView);
myGraphLayout.setRows(myCycleEditTabs.getTabCount() - 1);
myGraphs.validate();
return cycle;
}
public static int getTabbIndex(JComponent component) {
Container tabb = component.getParent();
Component lastComponent = component;
while (!(tabb instanceof JTabbedPane)) {
lastComponent = tabb;
tabb = tabb.getParent();
}
return ((JTabbedPane) tabb).indexOfComponent(lastComponent);
}
void removeCurrentCycle() {
int i = myCycleEditTabs.getSelectedIndex();
removeCycle(myCycleSetModel.myCycles.get(i));
}
void removeCycle(CycleSetModel.Cycle cycle) {
if (myCycleEditTabs.getTabCount() < 3) { // cant remove the last one
return;
}
if (cycle.myControl != null) {
myCycleEditTabs.remove(getTabbIndex(cycle.myControl));
}
myGraphs.remove(cycle.myView);
myGraphs.validate();
myGraphLayout.setRows(myCycleEditTabs.getTabCount() - 1);
myCycleSetModel.removeCycle(cycle);
myCycleEditTabs.setSelectedIndex(0);
}
public static void main(String[] arg) {
MainPanel f = new MainPanel();
f.setVisible(true);
}
}