|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.androidstudio.motionlayoutcycles; |
| 17 | + |
| 18 | +import static javax.swing.SwingConstants.TRAILING; |
| 19 | + |
| 20 | +import java.awt.BorderLayout; |
| 21 | +import java.awt.Container; |
| 22 | +import java.awt.GridBagConstraints; |
| 23 | +import java.awt.GridBagLayout; |
| 24 | +import java.awt.Insets; |
| 25 | +import java.awt.event.ActionListener; |
| 26 | +import java.awt.event.MouseAdapter; |
| 27 | +import java.awt.event.MouseEvent; |
| 28 | +import javax.swing.JButton; |
| 29 | +import javax.swing.JComboBox; |
| 30 | +import javax.swing.JLabel; |
| 31 | +import javax.swing.JPanel; |
| 32 | +import javax.swing.JSlider; |
| 33 | +import javax.swing.JTabbedPane; |
| 34 | +import javax.swing.JTextField; |
| 35 | +import javax.swing.UIManager; |
| 36 | +import javax.swing.border.EmptyBorder; |
| 37 | +import javax.swing.plaf.basic.BasicArrowButton; |
| 38 | + |
| 39 | +/** |
| 40 | + * The gui to edit a single cycle |
| 41 | + */ |
| 42 | +public class CycleEdit extends JPanel { |
| 43 | + |
| 44 | + private static final boolean DEBUG = false; |
| 45 | + JComboBox<String> baseMovement = new JComboBox<>(AnimationPanel.MOVE_NAMES); |
| 46 | + JComboBox<String> duration = new JComboBox<>(AnimationPanel.DURATION); |
| 47 | + CycleModel myCycleModel; |
| 48 | + JComboBox<String> attrName; |
| 49 | + |
| 50 | + class TabUI extends JPanel { |
| 51 | + |
| 52 | + JButton tabButton = new JButton(); |
| 53 | + JButton tabTitle = new JButton(); |
| 54 | + |
| 55 | + TabUI(String name) { |
| 56 | + super(new BorderLayout()); |
| 57 | + setBackground(null); |
| 58 | + tabButton.setBorder(null); |
| 59 | + |
| 60 | + add(tabTitle, BorderLayout.CENTER); |
| 61 | + add(tabButton, BorderLayout.EAST); |
| 62 | + tabTitle.setBorder(null); |
| 63 | + tabTitle.setFocusPainted(false); |
| 64 | + tabTitle.setContentAreaFilled(false); |
| 65 | + tabTitle.setBorderPainted(true); |
| 66 | + tabTitle.setBackground(null); |
| 67 | + tabTitle.setMargin(new Insets(0, 0, 0, 0)); |
| 68 | + tabTitle.setText(name); |
| 69 | + |
| 70 | + tabButton.setBorder(null); |
| 71 | + tabButton.setIcon(UIManager.getIcon("InternalFrame.paletteCloseIcon")); |
| 72 | + tabButton.setFocusPainted(false); |
| 73 | + tabButton.setContentAreaFilled(false); |
| 74 | + tabButton.setBorderPainted(true); |
| 75 | + tabButton.setBackground(null); |
| 76 | + tabButton.setMargin(new Insets(0, 0, 0, 0)); |
| 77 | + } |
| 78 | + |
| 79 | + public void addRaiseAction(ActionListener al) { |
| 80 | + tabTitle.addActionListener(al); |
| 81 | + } |
| 82 | + |
| 83 | + public void addKillAction(ActionListener al) { |
| 84 | + tabButton.addActionListener(al); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void updateTabName(String name) { |
| 89 | + if (DEBUG) { |
| 90 | + System.out.println(">>>>>" + name + " " + MainPanel.getTabbIndex(this)); |
| 91 | + StackTraceElement[] s = new Throwable().getStackTrace(); |
| 92 | + System.out.println(" .(" + s[1].getFileName() + ":" + s[1].getLineNumber() + ") "); |
| 93 | + System.out.println(" .(" + s[2].getFileName() + ":" + s[2].getLineNumber() + ") "); |
| 94 | + } |
| 95 | + Container tabb = getParent(); |
| 96 | + while (!(tabb instanceof JTabbedPane)) { |
| 97 | + tabb = tabb.getParent(); |
| 98 | + } |
| 99 | + |
| 100 | + JTabbedPane tabbedPane = (JTabbedPane) tabb; |
| 101 | + int index = MainPanel.getTabbIndex(this); |
| 102 | + tabbedPane.setTitleAt(index, name); |
| 103 | + // tabUI = new TabUI(name); |
| 104 | + // tabbedPane.setTabComponentAt(index, tabUI); |
| 105 | + } |
| 106 | + |
| 107 | + public void removeCycle() { |
| 108 | + myCycleModel.myCycle.myModelSet.removeCycle(myCycleModel.myCycle); |
| 109 | + listener.actionPerformed(null); |
| 110 | + } |
| 111 | + |
| 112 | + ActionListener listener; |
| 113 | + |
| 114 | + public void setRemoveCallback(ActionListener l) { |
| 115 | + listener = l; |
| 116 | + } |
| 117 | + |
| 118 | + public CycleEdit(CycleView cycleView, CycleModel cycleModel, AnimationPanel animationPanel) { |
| 119 | + super(new GridBagLayout()); |
| 120 | + JPanel control = this; |
| 121 | + |
| 122 | + setBorder(new EmptyBorder(5, 10, 10, 20)); |
| 123 | + myCycleModel = cycleModel; |
| 124 | + |
| 125 | + cycleView.addMouseListener(new MouseAdapter() { |
| 126 | + @Override |
| 127 | + public void mouseClicked(MouseEvent e) { |
| 128 | + myCycleModel.selectClosest(cycleView.last_click); |
| 129 | + } |
| 130 | + }); |
| 131 | + BasicArrowButton next = new BasicArrowButton(BasicArrowButton.EAST); |
| 132 | + BasicArrowButton prev = new BasicArrowButton(BasicArrowButton.WEST); |
| 133 | + myCycleModel.delete = next; |
| 134 | + cycleView.addMouseListener(new MouseAdapter() { |
| 135 | + @Override |
| 136 | + public void mouseClicked(MouseEvent e) { |
| 137 | + myCycleModel.selectClosest(cycleView.last_click); |
| 138 | + } |
| 139 | + }); |
| 140 | + JTextField number = new JTextField("XX"); |
| 141 | + number.setPreferredSize(number.getPreferredSize()); |
| 142 | + GridBagConstraints gbc = new GridBagConstraints(); |
| 143 | + number.setText("" + myCycleModel.selected); |
| 144 | + number.setEditable(false); |
| 145 | + gbc.insets = new Insets(10, 5, 0, 0); |
| 146 | + next.addActionListener((e) -> { |
| 147 | + myCycleModel.changeSelection(+1); |
| 148 | + }); |
| 149 | + prev.addActionListener((e) -> { |
| 150 | + myCycleModel.changeSelection(-1); |
| 151 | + }); |
| 152 | + myCycleModel.mKeyCycleNo = number; |
| 153 | + |
| 154 | + gbc.gridy = 0; |
| 155 | + gbc.gridx = 2; |
| 156 | + gbc.gridwidth = 1; |
| 157 | + |
| 158 | + control.add(new JLabel("KeyCycle:", TRAILING), gbc); |
| 159 | + gbc.gridx = 3; |
| 160 | + gbc.anchor = GridBagConstraints.EAST; |
| 161 | + control.add(prev, gbc); |
| 162 | + gbc.gridx++; |
| 163 | + control.add(number, gbc); |
| 164 | + gbc.gridx++; |
| 165 | + gbc.anchor = GridBagConstraints.WEST; |
| 166 | + control.add(next, gbc); |
| 167 | + gbc.gridy++; |
| 168 | + |
| 169 | + JButton del = new JButton("Delete"); |
| 170 | + JButton add = new JButton("Add"); |
| 171 | + myCycleModel.add = add; |
| 172 | + myCycleModel.delete = del; |
| 173 | + del.addActionListener((e) -> { |
| 174 | + myCycleModel.delete(); |
| 175 | + }); |
| 176 | + add.addActionListener((e) -> { |
| 177 | + myCycleModel.add(); |
| 178 | + }); |
| 179 | + gbc.gridwidth = 1; |
| 180 | + gbc.gridx = 3; |
| 181 | + control.add(add, gbc); |
| 182 | + gbc.gridx += 2; |
| 183 | + control.add(del, gbc); |
| 184 | + gbc.gridy++; |
| 185 | + gbc.gridwidth = 3; |
| 186 | + JSlider pos = new JSlider(); |
| 187 | + gbc.gridx = 0; |
| 188 | + gbc.anchor = GridBagConstraints.NORTHEAST; |
| 189 | + control.add(new JLabel("Pos:", TRAILING), gbc); |
| 190 | + gbc.gridx += 3; |
| 191 | + gbc.anchor = GridBagConstraints.WEST; |
| 192 | + control.add(pos, gbc); |
| 193 | + gbc.gridy++; |
| 194 | + |
| 195 | + JSlider period = new JSlider(); |
| 196 | + gbc.gridx = 0; |
| 197 | + gbc.anchor = GridBagConstraints.NORTHEAST; |
| 198 | + control.add(new JLabel("Period:", TRAILING), gbc); |
| 199 | + gbc.gridx += 3; |
| 200 | + gbc.anchor = GridBagConstraints.WEST; |
| 201 | + |
| 202 | + control.add(period, gbc); |
| 203 | + gbc.gridy++; |
| 204 | + |
| 205 | + JSlider amp = new JSlider(); |
| 206 | + gbc.gridx = 0; |
| 207 | + gbc.anchor = GridBagConstraints.NORTHEAST; |
| 208 | + attrName = new JComboBox<>(CycleView.MainAttribute.ShortNames); |
| 209 | + attrName.setSelectedIndex(myCycleModel.mAttrIndex); |
| 210 | + attrName.addActionListener(e -> { |
| 211 | + myCycleModel.setAttr(attrName.getSelectedIndex()); |
| 212 | + updateTabName(myCycleModel.getAttName()); |
| 213 | + animationPanel.setMode(); |
| 214 | + }); |
| 215 | + |
| 216 | + control.add(attrName, gbc); |
| 217 | + |
| 218 | + gbc.gridx += 3; |
| 219 | + gbc.anchor = GridBagConstraints.WEST; |
| 220 | + control.add(amp, gbc); |
| 221 | + |
| 222 | + gbc.gridy++; |
| 223 | + JSlider off = new JSlider(); |
| 224 | + gbc.gridx = 0; |
| 225 | + gbc.anchor = GridBagConstraints.NORTHEAST; |
| 226 | + control.add(new JLabel("Offset:", TRAILING), gbc); |
| 227 | + |
| 228 | + gbc.gridx += 3; |
| 229 | + gbc.anchor = GridBagConstraints.WEST; |
| 230 | + control.add(off, gbc); |
| 231 | + |
| 232 | + gbc.gridy++; |
| 233 | + gbc.gridx = 0; |
| 234 | + gbc.anchor = GridBagConstraints.EAST; |
| 235 | + control.add(new JLabel("WaveType:", TRAILING), gbc); |
| 236 | + |
| 237 | + JComboBox<String> mode = new JComboBox<>(myCycleModel.waveShapeName); |
| 238 | + gbc.gridx += 3; |
| 239 | + gbc.anchor = GridBagConstraints.WEST; |
| 240 | + control.add(mode, gbc); |
| 241 | + |
| 242 | + gbc.gridy++; |
| 243 | + gbc.gridx = 0; |
| 244 | + gbc.anchor = GridBagConstraints.NORTHEAST; |
| 245 | + control.add(new JLabel("Target:", TRAILING), gbc); |
| 246 | + |
| 247 | + gbc.gridx += 3; |
| 248 | + gbc.anchor = GridBagConstraints.WEST; |
| 249 | + JTextField target = new JTextField("XXXXXXXXXXXXXXXXXXXXX"); |
| 250 | + target.setPreferredSize(target.getPreferredSize()); |
| 251 | + target.setText("button"); |
| 252 | + control.add(target, gbc); |
| 253 | + |
| 254 | + myCycleModel.setTarget(target); |
| 255 | + myCycleModel.setUIElements(pos, period, amp, off, mode); |
| 256 | + myCycleModel.setCycle(cycleView); |
| 257 | + } |
| 258 | +} |
0 commit comments