GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — processing4/java/src/processing/mode/java/Rename.java at main · D-I-R-M/processing4 Expand file tree/ Copy pathRename.javaMore file actions 319 lines (265 loc) · 10.4 KB / Copy pathRename.javaFile metadata and controls319 lines (265 loc) · 10.4 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 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 package processing.mode.java; import static processing.mode.java.ASTUtils.findAllOccurrences; import static processing.mode.java.ASTUtils.getSimpleNameAt; import static processing.mode.java.ASTUtils.resolveBinding; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.WindowConstants; import javax.swing.border.EmptyBorder; import javax.swing.text.BadLocationException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.IBinding; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.SimpleName; import processing.app.Language; import processing.app.Platform; import processing.app.Sketch; import processing.app.SketchCode; import processing.app.syntax.SyntaxDocument; import processing.app.ui.EditorStatus; import processing.app.ui.Toolkit; class Rename { final JavaEditor editor; final PreprocService pps; final ShowUsage showUsage; final JDialog window; final JTextField textField; final JLabel oldNameLabel; IBinding binding; PreprocSketch ps; Rename(JavaEditor editor, PreprocService pps, ShowUsage showUsage) { this.editor = editor; this.pps = pps; this.showUsage = showUsage; // Add rename option JMenuItem renameItem = new JMenuItem(Language.text("editor.popup.rename")); renameItem.addActionListener(e -> handleRename()); editor.getTextArea().getRightClickPopup().add(renameItem); window = new JDialog(editor); JRootPane rootPane = window.getRootPane(); window.setTitle("Rename"); window.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); Toolkit.registerWindowCloseKeys(rootPane, e -> window.setVisible(false)); Toolkit.setIcon(window); window.setModal(true); window.setResizable(false); window.addComponentListener(new ComponentAdapter() { @Override public void componentHidden(ComponentEvent e) { binding = null; ps = null; } }); Box windowBox = Box.createVerticalBox(); windowBox.setBorder(new EmptyBorder(20, 20, 20, 20)); final int GAP = Toolkit.zoom(5); { // old name Box oldBox = Box.createHorizontalBox(); oldNameLabel = new JLabel("Current Name: "); oldBox.add(oldNameLabel); //oldBox.add(Box.createHorizontalStrut(10)); oldBox.add(Box.createHorizontalGlue()); windowBox.add(oldBox); windowBox.add(Box.createVerticalStrut(GAP)); } { // new name Box newBox = Box.createHorizontalBox(); JLabel newNameLabel = new JLabel("New Name: "); newBox.add(newNameLabel); newBox.add(textField = new JTextField(20)); newBox.add(Box.createHorizontalGlue()); windowBox.add(newBox); windowBox.add(Box.createVerticalStrut(GAP*2)); } { // button panel JButton showUsageButton = new JButton("Show Usage"); showUsageButton.addActionListener(e -> { showUsage.findUsageAndUpdateTree(ps, binding); window.setVisible(false); }); JButton renameButton = new JButton("Rename"); renameButton.addActionListener(e -> { final String newName = textField.getText().trim(); if (!newName.isEmpty()) { if (newName.chars().limit(1).allMatch(Character::isJavaIdentifierStart) && newName.chars().skip(1).allMatch(Character::isJavaIdentifierPart)) { rename(ps, binding, newName); window.setVisible(false); } else { String msg = String.format("'%s' is not a valid name", newName); JOptionPane.showMessageDialog(editor, msg, "Naming is Hard", JOptionPane.PLAIN_MESSAGE); } } }); rootPane.setDefaultButton(renameButton); Box buttonBox = Box.createHorizontalBox(); buttonBox.add(Box.createHorizontalGlue()); buttonBox.add(showUsageButton); if (!Platform.isMacOS()) { buttonBox.add(Box.createHorizontalStrut(GAP)); } buttonBox.add(renameButton); buttonBox.add(Box.createHorizontalGlue()); Dimension showDim = showUsageButton.getPreferredSize(); Dimension renameDim = renameButton.getPreferredSize(); final int niceSize = Math.max(showDim.width, renameDim.width) + GAP; final Dimension buttonDim = new Dimension(niceSize, showDim.height); showUsageButton.setPreferredSize(buttonDim); renameButton.setPreferredSize(buttonDim); windowBox.add(buttonBox); //window.add(panelBottom); } window.add(windowBox); window.pack(); //window.setMinimumSize(window.getSize()); } // Thread: EDT void handleRename() { int startOffset = editor.getSelectionStart(); int stopOffset = editor.getSelectionStop(); int tabIndex = editor.getSketch().getCurrentCodeIndex(); pps.whenDoneBlocking(ps -> handleRename(ps, tabIndex, startOffset, stopOffset)); } // Thread: worker void handleRename(PreprocSketch ps, int tabIndex, int startTabOffset, int stopTabOffset) { if (ps.hasSyntaxErrors) { editor.statusMessage("Cannot rename until syntax errors are fixed", EditorStatus.WARNING); return; } ASTNode root = ps.compilationUnit; // Map offsets int startJavaOffset = ps.tabOffsetToJavaOffset(tabIndex, startTabOffset); int stopJavaOffset = ps.tabOffsetToJavaOffset(tabIndex, stopTabOffset); // Find the node SimpleName name = getSimpleNameAt(root, startJavaOffset, stopJavaOffset); if (name == null) { editor.statusMessage("Highlight the class/function/variable name first", EditorStatus.NOTICE); return; } // Find binding IBinding binding = resolveBinding(name); if (binding == null) { editor.statusMessage(name.getIdentifier() + " isn't defined in this sketch, " + "so it cannot be renamed", EditorStatus.ERROR); return; } ASTNode decl = ps.compilationUnit.findDeclaringNode(binding.getKey()); if (decl == null) { editor.statusMessage(name.getIdentifier() + " isn't defined in this sketch, " + "so it cannot be renamed", EditorStatus.ERROR); return; } // Display the rename dialog EventQueue.invokeLater(() -> { if (!window.isVisible()) { this.ps = ps; this.binding = binding; oldNameLabel.setText("Current name: " + binding.getName()); textField.setText(binding.getName()); textField.requestFocus(); textField.selectAll(); int x = editor.getX() + (editor.getWidth() - window.getWidth()) / 2; int y = editor.getY() + (editor.getHeight() - window.getHeight()) / 2; window.setLocation(x, y); window.setVisible(true); window.toFront(); } }); } // Thread: EDT (we can't allow user to mess with sketch while renaming) void rename(PreprocSketch ps, IBinding binding, String newName) { CompilationUnit root = ps.compilationUnit; // Renaming constructor should rename class if (binding.getKind() == IBinding.METHOD) { IMethodBinding method = (IMethodBinding) binding; if (method.isConstructor()) { binding = method.getDeclaringClass(); } } ASTNode decl = root.findDeclaringNode(binding.getKey()); if (decl == null) return; showUsage.hide(); List<SimpleName> occurrences = new ArrayList<>(findAllOccurrences(root, binding.getKey())); // Renaming class should rename all constructors if (binding.getKind() == IBinding.TYPE) { ITypeBinding type = (ITypeBinding) binding; //type = type.getErasure(); IMethodBinding[] methods = type.getDeclaredMethods(); Arrays.stream(methods) .filter(IMethodBinding::isConstructor) .flatMap(c -> findAllOccurrences(root, c.getKey()).stream()) .forEach(occurrences::add); } Map<Integer, List<SketchInterval>> mappedNodes = occurrences.stream() .map(ps::mapJavaToSketch) .filter(ps::inRange) .collect(Collectors.groupingBy(interval -> interval.tabIndex)); Sketch sketch = ps.sketch; editor.startCompoundEdit(); mappedNodes.forEach((key, nodes) -> { int tabIndex = key; SketchCode sketchCode = sketch.getCode(tabIndex); SyntaxDocument document = (SyntaxDocument) sketchCode.getDocument(); nodes.stream() // Replace from the end so all unprocessed offsets stay valid .sorted(Comparator.comparing((SketchInterval si) -> si.startTabOffset).reversed()) .forEach(si -> { // Make sure offsets are in bounds int documentLength = document.getLength(); if (si.startTabOffset >= 0 && si.startTabOffset <= documentLength && si.stopTabOffset >= 0 && si.stopTabOffset <= documentLength) { // Replace the code int length = si.stopTabOffset - si.startTabOffset; try { document.remove(si.startTabOffset, length); document.insertString(si.startTabOffset, newName, null); } catch (BadLocationException e) { /* Whatever */ } } }); try { sketchCode.setProgram(document.getText(0, document.getLength())); } catch (BadLocationException e) { /* Whatever */ } sketchCode.setModified(true); }); editor.stopCompoundEdit(); editor.repaintHeader(); int currentTabIndex = sketch.getCurrentCodeIndex(); final int currentOffset = editor.getCaretOffset(); int precedingIntervals = (int) mappedNodes.getOrDefault(currentTabIndex, Collections.emptyList()) .stream() .filter(interval -> interval.stopTabOffset < currentOffset) .count(); int intervalLengthDiff = newName.length() - binding.getName().length(); int offsetDiff = precedingIntervals * intervalLengthDiff; editor.getTextArea().setCaretPosition(currentOffset + offsetDiff); } void dispose() { if (window != null) { window.dispose(); } } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |