matthiasblaesing commented on code in PR #7938: URL: https://github.com/apache/netbeans/pull/7938#discussion_r1898019389
########## ide/svg/src/org/netbeans/modules/svg/SVGDataObject.java: ########## @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.netbeans.modules.svg; + +import org.netbeans.core.spi.multiview.MultiViewElement; +import org.netbeans.core.spi.multiview.text.MultiViewEditorElement; +import org.netbeans.modules.xml.XMLDataObjectLook; +import org.netbeans.modules.xml.cookies.DataObjectCookieManager; +import org.netbeans.modules.xml.cookies.UpdateDocumentCookie; +import org.netbeans.modules.xml.sync.DataObjectSyncSupport; +import org.netbeans.modules.xml.sync.Synchronizator; +import org.netbeans.spi.xml.cookies.CheckXMLSupport; +import org.netbeans.spi.xml.cookies.DataObjectAdapters; +import org.netbeans.spi.xml.cookies.ValidateXMLSupport; +import org.openide.awt.ActionID; +import org.openide.awt.ActionReference; +import org.openide.awt.ActionReferences; +import org.openide.awt.HtmlBrowser; +import org.openide.cookies.ViewCookie; +import org.openide.filesystems.*; +import org.openide.loaders.*; +import org.openide.nodes.CookieSet; +import org.openide.nodes.Node; +import org.openide.util.*; +import org.openide.windows.TopComponent; +import org.xml.sax.InputSource; + +/** + * + * @author Christian Lenz + */ [email protected]({ + "LBL_SVG_LOADER=Files of SVG" +}) [email protected]( + displayName = "#LBL_SVG_LOADER", + mimeType = SVGDataObject.MIME_TYPE, + extension = {"svg", "SVG"}, + position = 20367 Review Comment: You should play lotto. There is not much in the 20.000-30.000 position range, you hit the one existing registration: ``` [exec] WARNING [org.openide.filesystems.Ordering]: Found same position 20.367 for both Services/MIMEResolver/org-netbeans-modules-markdown-MarkdownDataObject-Extension.xml and Services/MIMEResolver/org-netbeans-modules-svg-SVGDataObject-Extension.xml ``` Lets move it a bit: ```suggestion position = 21367 ``` ########## ide/svg/src/org/netbeans/modules/svg/toolbar/actions/CustomZoomAction.java: ########## @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.netbeans.modules.svg.toolbar.actions; + +import java.awt.Dialog; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import org.netbeans.modules.svg.SVGViewerElement; + +import org.openide.*; +import org.openide.awt.ActionID; +import org.openide.awt.ActionRegistration; +import org.openide.util.actions.CallableSystemAction; +import org.openide.util.HelpCtx; +import org.openide.util.NbBundle; + +/** + * Action that can always be invoked and work procedurally. + * + * @author Christian Lenz + */ +@ActionID(id = "org.netbeans.modules.svg.toolbar.CustomZoomAction", category = "View") +@ActionRegistration(lazy = false, displayName = "#LBL_CustomZoom") +public class CustomZoomAction extends CallableSystemAction { Review Comment: The actions are broken. You are invoking `new CustomZoomAction`, this must not be done. SystemAction instances can be fetched using `SystemAction.get(CustomZoomAction.class)` ########## ide/svg/src/org/netbeans/modules/svg/navigation/SVGNavigatorPanel.java: ########## @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.netbeans.modules.svg.navigation; + +import com.github.weisj.jsvg.SVGDocument; +import com.github.weisj.jsvg.parser.SVGLoader; +import java.io.IOException; +import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JComponent; +import javax.swing.SwingUtilities; +import org.netbeans.modules.svg.SVGDataObject; +import org.netbeans.modules.svg.SVGViewerElement; +import org.netbeans.spi.navigator.NavigatorPanel; +import org.openide.filesystems.FileChangeAdapter; +import org.openide.filesystems.FileChangeListener; +import org.openide.filesystems.FileEvent; +import org.openide.filesystems.FileObject; +import org.openide.loaders.DataObject; +import org.openide.loaders.DataObjectNotFoundException; +import org.openide.util.*; + +/** + * + * @author christian lenz + */ [email protected]({ + @NavigatorPanel.Registration(mimeType = "image/svg+xml", displayName = "#Navigator_DisplayName") +}) +public class SVGNavigatorPanel implements NavigatorPanel { + + private static final Logger LOG = Logger.getLogger(SVGViewerElement.class.getName()); + + /** + * holds UI of this panel + */ + private SVGPreviewPanel panelUI; + /** + * template for finding data in given context. Object used as example, + * replace with your own data source, for example JavaDataObject etc + */ + private static final Lookup.Template<SVGDataObject> MY_DATA = new Lookup.Template<>(SVGDataObject.class); + /** + * current context to work on + */ + private Lookup.Result<SVGDataObject> currentContext; + /** + * listener to context changes + */ + private LookupListener contextListener; + /** + * Listens for changes on image file. + */ + private FileChangeListener fileChangeListener; + private long lastSaveTime = -1; + private DataObject currentDataObject; + private static final RequestProcessor WORKER = new RequestProcessor(SVGNavigatorPanel.class.getName()); + + @Override + public String getDisplayName() { + return NbBundle.getMessage(SVGNavigatorPanel.class, "Navigator_DisplayName"); + } + + @Override + public String getDisplayHint() { + return NbBundle.getMessage(SVGNavigatorPanel.class, "Navigator_DisplayHint"); + } + + @Override + public JComponent getComponent() { + if (lastSaveTime == -1) { + lastSaveTime = System.currentTimeMillis(); + } + if (panelUI == null) { + panelUI = new SVGPreviewPanel(); + } + return panelUI; + } + + @Override + public void panelActivated(Lookup context) { + // lookup context and listen to result to get notified about context changes + currentContext = context.lookup(MY_DATA); + currentContext.addLookupListener(getContextListener()); + // get actual data and recompute content + Collection<? extends SVGDataObject> data = currentContext.allInstances(); + currentDataObject = getDataObject(data); + + if (currentDataObject == null) { + return; + } + + if (fileChangeListener == null) { + fileChangeListener = new SVGFileChangeAdapter(); + } + currentDataObject.getPrimaryFile().addFileChangeListener(fileChangeListener); + setNewContent(currentDataObject); + } + + @Override + public void panelDeactivated() { + currentContext.removeLookupListener(getContextListener()); + currentContext = null; + if (currentDataObject != null) { + currentDataObject.getPrimaryFile().removeFileChangeListener(fileChangeListener); + } + currentDataObject = null; + } + + @Override + public Lookup getLookup() { + // go with default activated Node strategy + return null; + } + + private void setNewContent(final DataObject dataObject) { + if (dataObject == null) { + return; + } + + WORKER.post(() -> { + FileObject fo = dataObject.getPrimaryFile(); + + if (fo == null) { + return; + } + + if (panelUI == null) { + return; Review Comment: Idea taken from the image module. If there is not yet a UI, create it now. That way there is no race between `setNewContent` and `getComponent`. ```suggestion getComponent(); ``` We are only interested in the side effect of that call and that ensures, that even the first selection of an SVG gets a navigator view. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
