From e5ad2c311a2ac9423f68afdd84d3c4002f62a8d4 Mon Sep 17 00:00:00 2001 From: mbrasil Date: Tue, 19 Mar 2024 16:10:48 +0000 Subject: [PATCH] chore: lint, warnings and cleanup [BACKLOG-40051] --- .../mantle/client/ui/PerspectiveManager.java | 237 +++++++++--------- 1 file changed, 125 insertions(+), 112 deletions(-) diff --git a/user-console/src/main/java/org/pentaho/mantle/client/ui/PerspectiveManager.java b/user-console/src/main/java/org/pentaho/mantle/client/ui/PerspectiveManager.java index 46cab1dc7ee..d8da2b06bd8 100644 --- a/user-console/src/main/java/org/pentaho/mantle/client/ui/PerspectiveManager.java +++ b/user-console/src/main/java/org/pentaho/mantle/client/ui/PerspectiveManager.java @@ -42,8 +42,8 @@ import com.google.gwt.user.client.ui.MenuItem; import com.google.gwt.user.client.ui.SimplePanel; import com.google.gwt.user.client.ui.Widget; +import org.pentaho.gwt.widgets.client.menuitem.MenuCloner; import org.pentaho.gwt.widgets.client.menuitem.PentahoMenuItem; -import org.pentaho.gwt.widgets.client.utils.i18n.IResourceBundleLoadCallback; import org.pentaho.gwt.widgets.client.utils.i18n.ResourceBundle; import org.pentaho.mantle.client.MantleApplication; import org.pentaho.mantle.client.MantleUtils; @@ -56,7 +56,6 @@ import org.pentaho.mantle.client.ui.xul.JsPerspective; import org.pentaho.mantle.client.ui.xul.JsXulOverlay; import org.pentaho.mantle.client.ui.xul.MantleXul; -import org.pentaho.gwt.widgets.client.menuitem.MenuCloner; import org.pentaho.platform.api.engine.perspective.pojo.IPluginPerspective; import org.pentaho.platform.plugin.services.pluginmgr.perspective.pojo.DefaultPluginPerspective; import org.pentaho.ui.xul.XulOverlay; @@ -64,40 +63,33 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; +@SuppressWarnings( { "deprecation", "unused" } ) public class PerspectiveManager extends SimplePanel { - private static final String ALLOW_TRANSPARENCY_ATTRIBUTE = "allowTransparency"; private static final String REMOVE_IFRAME_BORDERS = "frameBorder"; - public static final String ADMIN_PERSPECTIVE = "admin.perspective"; public static final String SCHEDULES_PERSPECTIVE = "schedules.perspective"; public static final String OPENED_PERSPECTIVE = "opened.perspective"; public static final String HOME_PERSPECTIVE = "home.perspective"; public static final String BROWSER_PERSPECTIVE = "browser.perspective"; - public static final String PROPERTIES_EXTENSION = ".properties"; //$NON-NLS-1$ public static final String SEPARATOR = "/"; //$NON-NLS-1$ - - private static PerspectiveManager instance = new PerspectiveManager(); - + private static final PerspectiveManager instance = new PerspectiveManager(); + public static final String CONTENT_TYPE = "Content-Type"; + public static final String APPLICATION_JSON = "application/json"; + public static final String IF_MODIFIED_SINCE = "If-Modified-Since"; + public static final String IF_MODIFIED_SINCE_DATE = "01 Jan 1970 00:00:00 GMT"; private CustomDropDown perspectiveDropDown; - private HashMap perspectiveMenuItemMap = new HashMap(); - + private final HashMap perspectiveMenuItemMap = new HashMap<>(); private PentahoMenuItem browserMenuItem; private PentahoMenuItem schedulesMenuItem; - // create an overlay list to later register with the main toolbar/menubar - private ArrayList overlays = new ArrayList(); + private final ArrayList overlays = new ArrayList<>(); private ArrayList perspectives; - private IPluginPerspective activePerspective; - private boolean loaded = false; - private boolean canSchedule; public static PerspectiveManager getInstance() { @@ -110,90 +102,101 @@ private PerspectiveManager() { init(); } - private Collection getMenuItems(){ + private Collection getMenuItems() { return perspectiveMenuItemMap.values(); } private void init() { - final String url = MantleUtils.getSchedulerPluginContextURL() + "api/scheduler/canSchedule?ts=" + System.currentTimeMillis(); //$NON-NLS-1$ + final String url = MantleUtils.getSchedulerPluginContextURL() + "api/scheduler/canSchedule?ts=" + + System.currentTimeMillis(); //$NON-NLS-1$ RequestBuilder builder = new RequestBuilder( RequestBuilder.GET, url ); - builder.setHeader( "Content-Type", "application/json" ); //$NON-NLS-1$//$NON-NLS-2$ - builder.setHeader( "If-Modified-Since", "01 Jan 1970 00:00:00 GMT" ); + builder.setHeader( CONTENT_TYPE, APPLICATION_JSON ); + builder.setHeader( IF_MODIFIED_SINCE, IF_MODIFIED_SINCE_DATE ); canSchedule = true; + try { builder.sendRequest( null, new RequestCallback() { @Override public void onError( Request request, Throwable throwable ) { setupPerspectiveManager(); } + @Override public void onResponseReceived( Request request, Response response ) { if ( response.getStatusCode() == 200 ) { canSchedule = "true".equals( response.getText() ); } + setupPerspectiveManager(); } } ); - } catch ( Exception e ) { + } catch ( Exception ignored ) { + // ignore } } private void setupPerspectiveManager() { - final String url = GWT.getHostPageBaseURL() + "api/plugin-manager/perspectives?ts=" + System.currentTimeMillis(); //$NON-NLS-1$ + final String url = + GWT.getHostPageBaseURL() + "api/plugin-manager/perspectives?ts=" + System.currentTimeMillis(); //$NON-NLS-1$ RequestBuilder builder = new RequestBuilder( RequestBuilder.GET, url ); - builder.setHeader( "Content-Type", "application/json" ); //$NON-NLS-1$//$NON-NLS-2$ - builder.setHeader( "If-Modified-Since", "01 Jan 1970 00:00:00 GMT" ); + builder.setHeader( CONTENT_TYPE, APPLICATION_JSON ); + builder.setHeader( IF_MODIFIED_SINCE, IF_MODIFIED_SINCE_DATE ); try { builder.sendRequest( null, new RequestCallback() { public void onError( Request request, Throwable exception ) { - Window.alert( "getPluginPerpectives fail: " + exception.getMessage() ); + Window.alert( "getPluginPerspectives fail: " + exception.getMessage() ); } public void onResponseReceived( Request request, Response response ) { + JsArray jsPerspectives = + JsPerspective.parseJson( JsonUtils.escapeJsonForEval( response.getText() ) ); + ArrayList pluginPerspectives = new ArrayList<>(); + + for ( int i = 0; i < jsPerspectives.length(); i++ ) { + JsPerspective jsperspective = jsPerspectives.get( i ); - JsArray jsperspectives = - JsPerspective.parseJson( JsonUtils.escapeJsonForEval( response.getText() ) ); - ArrayList perspectives = new ArrayList(); - for ( int i = 0; i < jsperspectives.length(); i++ ) { - JsPerspective jsperspective = jsperspectives.get( i ); // Don't include schedules if user doesn't have schedule permissions if ( "${schedules}".equals( jsperspective.getTitle() ) && !canSchedule ) { continue; } + DefaultPluginPerspective perspective = new DefaultPluginPerspective(); perspective.setContentUrl( jsperspective.getContentUrl() ); perspective.setId( jsperspective.getId() ); perspective.setLayoutPriority( Integer.parseInt( jsperspective.getLayoutPriority() ) ); - ArrayList requiredSecurityActions = new ArrayList(); + ArrayList requiredSecurityActions = new ArrayList<>(); + if ( jsperspective.getRequiredSecurityActions() != null ) { for ( int j = 0; j < jsperspective.getRequiredSecurityActions().length(); j++ ) { requiredSecurityActions.add( jsperspective.getRequiredSecurityActions().get( j ) ); } } - // will need to iterate over jsoverlays and convert to MantleXulOverlay - ArrayList overlays = new ArrayList(); + // will need to iterate over jsOverlays and convert to MantleXulOverlay + ArrayList xulOverlays = new ArrayList<>(); + if ( jsperspective.getOverlays() != null ) { for ( int j = 0; j < jsperspective.getOverlays().length(); j++ ) { JsXulOverlay o = jsperspective.getOverlays().get( j ); MantleXulOverlay overlay = - new MantleXulOverlay( o.getId(), o.getOverlayUri(), o.getSource(), o.getResourceBundleUri() ); - overlays.add( overlay ); + new MantleXulOverlay( o.getId(), o.getOverlayUri(), o.getSource(), o.getResourceBundleUri() ); + xulOverlays.add( overlay ); } } - perspective.setOverlays( overlays ); + + perspective.setOverlays( xulOverlays ); perspective.setRequiredSecurityActions( requiredSecurityActions ); perspective.setResourceBundleUri( jsperspective.getResourceBundleUri() ); perspective.setTitle( jsperspective.getTitle() ); - perspectives.add( perspective ); + pluginPerspectives.add( perspective ); } - setPluginPerspectives( perspectives ); + setPluginPerspectives( pluginPerspectives ); } } ); } catch ( RequestException e ) { @@ -204,18 +207,15 @@ public void onResponseReceived( Request request, Response response ) { } protected void setPluginPerspectives( final ArrayList perspectives ) { - this.perspectives = perspectives; clear(); // sort perspectives - Collections.sort( perspectives, new Comparator() { - public int compare( IPluginPerspective o1, IPluginPerspective o2 ) { - Integer p1 = new Integer( o1.getLayoutPriority() ); - Integer p2 = new Integer( o2.getLayoutPriority() ); - return p1.compareTo( p2 ); - } + perspectives.sort( ( o1, o2 ) -> { + Integer p1 = o1.getLayoutPriority(); + Integer p2 = o2.getLayoutPriority(); + return p1.compareTo( p2 ); } ); MenuBar perspectiveMenuBar = new MenuBar( true ); @@ -223,13 +223,10 @@ public int compare( IPluginPerspective o1, IPluginPerspective o2 ) { setWidget( perspectiveDropDown ); loadResourceBundle( perspectiveDropDown, perspectives.get( 0 ) ); - ScheduledCommand noopCmd = new ScheduledCommand() { - public void execute() { - } + ScheduledCommand noopCmd = () -> { }; for ( final IPluginPerspective perspective : perspectives ) { - // if we have overlays add it to the list if ( perspective.getOverlays() != null ) { overlays.addAll( perspective.getOverlays() ); @@ -237,12 +234,10 @@ public void execute() { final MenuItem menuItem = new MenuItem( "", noopCmd ); perspectiveMenuItemMap.put( perspective.getId(), menuItem ); - ScheduledCommand cmd = new ScheduledCommand() { - public void execute() { - showPerspective( perspective ); - perspectiveDropDown.setText( menuItem.getText() ); - perspectiveDropDown.hidePopup(); - } + ScheduledCommand cmd = () -> { + showPerspective( perspective ); + perspectiveDropDown.setText( menuItem.getText() ); + perspectiveDropDown.hidePopup(); }; menuItem.setScheduledCommand( cmd ); perspectiveMenuBar.addItem( menuItem ); @@ -261,14 +256,16 @@ public void execute() { private void loadResourceBundle( final HasText textWidget, final IPluginPerspective perspective ) { try { String bundle = perspective.getResourceBundleUri(); + if ( bundle == null ) { return; } + String folder = ""; //$NON-NLS-1$ String baseName = bundle; // we have to separate the folder from the base name - if ( bundle.indexOf( SEPARATOR ) > -1 ) { + if ( bundle.contains( SEPARATOR ) ) { folder = bundle.substring( 0, bundle.lastIndexOf( SEPARATOR ) + 1 ); baseName = bundle.substring( bundle.lastIndexOf( SEPARATOR ) + 1 ); } @@ -277,23 +274,21 @@ private void loadResourceBundle( final HasText textWidget, final IPluginPerspect if ( baseName.contains( PROPERTIES_EXTENSION ) ) { baseName = baseName.substring( 0, baseName.indexOf( PROPERTIES_EXTENSION ) ); } + // some may put the .properties on incorrectly - if ( baseName.contains( ".properties" ) ) { - baseName = baseName.substring( 0, baseName.indexOf( ".properties" ) ); + if ( baseName.contains( PROPERTIES_EXTENSION ) ) { + baseName = baseName.substring( 0, baseName.indexOf( PROPERTIES_EXTENSION ) ); } final ResourceBundle messageBundle = new ResourceBundle(); - messageBundle.loadBundle( folder, baseName, true, new IResourceBundleLoadCallback() { - public void bundleLoaded( String arg0 ) { - String title = ResourceBundleTranslator.translate( perspective.getTitle(), messageBundle ); - perspective.setTitle( title ); - textWidget.setText( title ); - } + messageBundle.loadBundle( folder, baseName, true, arg0 -> { + String title = ResourceBundleTranslator.translate( perspective.getTitle(), messageBundle ); + perspective.setTitle( title ); + textWidget.setText( title ); } ); - } catch ( Throwable t ) { - Window.alert( "Error loading message bundle: " + t.getMessage() ); //$NON-NLS-1$ - t.printStackTrace(); + } catch ( Exception e ) { + Window.alert( "Error loading message bundle: " + e.getMessage() ); //$NON-NLS-1$ } } @@ -301,27 +296,26 @@ public void enablePerspective( final String perspectiveId, boolean enabled ) { if ( perspectives == null ) { return; } + // return value to indicate if perspective now disabled - for ( int i = 0; i < perspectives.size(); i++ ) { - if ( perspectives.get( i ).getId().equalsIgnoreCase( perspectiveId ) ) { - perspectiveMenuItemMap.get( perspectiveId ).setEnabled( enabled ); - return; - } + if ( perspectives.stream().anyMatch( perspective -> perspective.getId().equalsIgnoreCase( perspectiveId ) ) ) { + perspectiveMenuItemMap.get( perspectiveId ).setEnabled( enabled ); } - return; } public boolean setPerspective( final String perspectiveId ) { if ( perspectives == null ) { return false; } + // return value to indicate if perspective now shown - for ( int i = 0; i < perspectives.size(); i++ ) { - if ( perspectives.get( i ).getId().equalsIgnoreCase( perspectiveId ) ) { - showPerspective( perspectives.get( i ) ); + for ( IPluginPerspective perspective : perspectives ) { + if ( perspective.getId().equalsIgnoreCase( perspectiveId ) ) { + showPerspective( perspective ); return true; } } + return false; } @@ -334,19 +328,21 @@ public void showPerspectiveWithHighestPriority() { } private void showPerspective( final IPluginPerspective perspective ) { - if ( activePerspective == perspective ) { return; } if ( activePerspective != null && ADMIN_PERSPECTIVE.equals( activePerspective.getId() ) ) { final Widget activeAdminPanel = MantleXul.getInstance().getAdminContentDeck().getWidget( MantleXul.getInstance() - .getAdminContentDeck().getVisibleWidget() ); + .getAdminContentDeck().getVisibleWidget() ); + if ( activeAdminPanel != null ) { if ( activeAdminPanel instanceof ISysAdminPanel ) { ( (ISysAdminPanel) activeAdminPanel ).passivate( new AsyncCallback() { @Override - public void onFailure( Throwable caught ) { } + public void onFailure( Throwable caught ) { + // ignore + } @Override public void onSuccess( Boolean result ) { @@ -366,15 +362,18 @@ private void showPerspectiveContinue( IPluginPerspective perspective ) { if ( !perspective.getTitle().startsWith( "${" ) ) { perspectiveDropDown.setText( perspective.getTitle() ); } + for ( MenuItem m : perspectiveMenuItemMap.values() ) { m.getElement().removeClassName( "custom-dropdown-selected" ); } + perspectiveMenuItemMap.get( perspective.getId() ).getElement().addClassName( "custom-dropdown-selected" ); - // before we show.. de-activate current perspective (based on shown widget) + // before we show de-activate current perspective (based on shown widget) Widget w = - MantleApplication.getInstance().getContentDeck().getWidget( - MantleApplication.getInstance().getContentDeck().getVisibleWidget() ); + MantleApplication.getInstance().getContentDeck().getWidget( + MantleApplication.getInstance().getContentDeck().getVisibleWidget() ); + if ( w instanceof Frame && !perspective.getId().equals( w.getElement().getId() ) ) { // invoke deactivation method Frame frame = (Frame) w; @@ -388,6 +387,7 @@ private void showPerspectiveContinue( IPluginPerspective perspective ) { MantleXul.getInstance().removeOverlay( o.getId() ); } } + for ( XulOverlay overlay : MantleXul.getInstance().getOverlays() ) { if ( overlay.getId().startsWith( activePerspective.getId() + ".overlay." ) ) { MantleXul.getInstance().removeOverlay( overlay.getId() ); @@ -405,6 +405,7 @@ private void showPerspectiveContinue( IPluginPerspective perspective ) { MantleXul.getInstance().applyOverlay( overlay.getId() ); } } + // handle PLUGIN overlays for ( XulOverlay overlay : MantleXul.getInstance().getOverlays() ) { if ( overlay.getId().startsWith( perspective.getId() + ".overlay." ) ) { @@ -414,7 +415,7 @@ private void showPerspectiveContinue( IPluginPerspective perspective ) { } if ( !perspective.getId().equals( OPENED_PERSPECTIVE ) && !perspective.getId().equals( SCHEDULES_PERSPECTIVE ) - && !perspective.getId().equals( ADMIN_PERSPECTIVE ) ) { + && !perspective.getId().equals( ADMIN_PERSPECTIVE ) ) { hijackContentArea( perspective ); } @@ -430,9 +431,11 @@ private void showPerspectiveContinue( IPluginPerspective perspective ) { private void showOpenedPerspective( boolean browserChecked, boolean schedulesChecked ) { DeckPanel contentDeck = MantleApplication.getInstance().getContentDeck(); + if ( MantleApplication.getInstance().getContentDeck().getWidgetIndex( SolutionBrowserPanel.getInstance() ) == -1 ) { contentDeck.add( SolutionBrowserPanel.getInstance() ); } + // show stuff we've created/configured contentDeck.showWidget( contentDeck.getWidgetIndex( SolutionBrowserPanel.getInstance() ) ); SolutionBrowserPanel.getInstance().setNavigatorShowing( SolutionBrowserPanel.getInstance().isNavigatorShowing() ); @@ -443,17 +446,16 @@ private void showSchedulesPerspective() { DeckPanel contentDeck = MantleApplication.getInstance().getContentDeck(); GWT.runAsync( new RunAsyncCallback() { - public void onSuccess() { // Use a SimplePanel to hold the returned element, // and add it as a child widget if it is not there yet. SimplePanel activePerspectiveWidget = (SimplePanel) findPerspectiveWidget( contentDeck, activePerspective ); boolean isFirstTime = activePerspectiveWidget == null; + if ( isFirstTime ) { // Setup activePerspectiveWidget = new SimplePanel(); activePerspectiveWidget.getElement().setId( activePerspective.getId() ); - contentDeck.add( activePerspectiveWidget ); } @@ -468,6 +470,7 @@ public void onSuccess() { } public void onFailure( Throwable reason ) { + // ignore } } ); @@ -476,8 +479,10 @@ public void onFailure( Throwable reason ) { private static Widget findPerspectiveWidget( DeckPanel contentDeck, IPluginPerspective perspective ) { int count = contentDeck.getWidgetCount(); + for ( int i = 0; i < count; i++ ) { Widget widget = contentDeck.getWidget( i ); + if ( perspective.getId().equals( widget.getElement().getId() ) ) { return widget; } @@ -488,10 +493,12 @@ private static Widget findPerspectiveWidget( DeckPanel contentDeck, IPluginPersp private void showAdminPerspective( boolean browserChecked, boolean schedulesChecked ) { DeckPanel contentDeck = MantleApplication.getInstance().getContentDeck(); + if ( MantleApplication.getInstance().getContentDeck() - .getWidgetIndex( MantleXul.getInstance().getAdminPerspective() ) == -1 ) { + .getWidgetIndex( MantleXul.getInstance().getAdminPerspective() ) == -1 ) { contentDeck.add( MantleXul.getInstance().getAdminPerspective() ); } + contentDeck.showWidget( contentDeck.getWidgetIndex( MantleXul.getInstance().getAdminPerspective() ) ); MantleXul.getInstance().customizeAdminStyle(); MantleXul.getInstance().configureAdminCatTree(); @@ -502,12 +509,15 @@ private void showAdminPerspective( boolean browserChecked, boolean schedulesChec private void hijackContentArea( IPluginPerspective perspective ) { // hijack content area (or simply find and select existing content) Frame frame = null; + for ( int i = 0; i < MantleApplication.getInstance().getContentDeck().getWidgetCount(); i++ ) { Widget w = MantleApplication.getInstance().getContentDeck().getWidget( i ); + if ( w instanceof Frame && perspective.getId().equals( w.getElement().getId() ) ) { frame = (Frame) w; } } + if ( frame == null ) { frame = new Frame( perspective.getContentUrl() ); Element frameElement = frame.getElement(); @@ -520,7 +530,7 @@ private void hijackContentArea( IPluginPerspective perspective ) { } MantleApplication.getInstance().getContentDeck().showWidget( - MantleApplication.getInstance().getContentDeck().getWidgetIndex( frame ) ); + MantleApplication.getInstance().getContentDeck().getWidgetIndex( frame ) ); final Element frameElement = frame.getElement(); perspectiveActivated( frameElement ); @@ -532,43 +542,45 @@ public native com.google.gwt.dom.client.Element getSchedulesPerspectiveElement( }-*/; private native void perspectiveActivated( Element frameElement ) - /*-{ - try { - frameElement.contentWindow.perspectiveActivated(); - } catch (e) { - } - }-*/; + /*-{ + try { + frameElement.contentWindow.perspectiveActivated(); + } catch (e) { + } + }-*/; private native void perspectiveDeactivated( Element frameElement ) - /*-{ - try { - frameElement.contentWindow.perspectiveDeactivated(); - } catch (e) { - } - }-*/; + /*-{ + try { + frameElement.contentWindow.perspectiveDeactivated(); + } catch (e) { + } + }-*/; private native void registerFunctions( PerspectiveManager manager ) - /*-{ - $wnd.mantle_getPerspectives = function() { - return manager.@org.pentaho.mantle.client.ui.PerspectiveManager::getPerspectives()(); - } - $wnd.mantle_setPerspective = function(perspectiveId) { - manager.@org.pentaho.mantle.client.ui.PerspectiveManager::setPerspective(Ljava/lang/String;)(perspectiveId); - } - }-*/; + /*-{ + $wnd.mantle_getPerspectives = function () { + return manager.@org.pentaho.mantle.client.ui.PerspectiveManager::getPerspectives()(); + } + $wnd.mantle_setPerspective = function (perspectiveId) { + manager.@org.pentaho.mantle.client.ui.PerspectiveManager::setPerspective(Ljava/lang/String;)(perspectiveId); + } + }-*/; private JsArrayString getPerspectives() { JsArrayString stringArray = getJsArrayString(); + for ( IPluginPerspective perspective : perspectives ) { stringArray.push( perspective.getId() ); } + return stringArray; } private native JsArrayString getJsArrayString() - /*-{ - return []; - }-*/; + /*-{ + return []; + }-*/; public IPluginPerspective getActivePerspective() { return activePerspective; @@ -608,7 +620,8 @@ public MenuItem getBurgerBarPerspectiveMenuItem() { MenuCloner menuCloner = new MenuCloner<>( m -> new BurgerMenuBar() ); getMenuItems().forEach( m -> perspectivesMenuBar.addItem( menuCloner.clone( m ) ) ); - MenuItem burgerBarPerspectiveMenuItem = new MenuItem( getActivePerspective().getTitle(), (Scheduler.ScheduledCommand) null ); + MenuItem burgerBarPerspectiveMenuItem = + new MenuItem( getActivePerspective().getTitle(), (Scheduler.ScheduledCommand) null ); burgerBarPerspectiveMenuItem.setSubMenu( perspectivesMenuBar ); return burgerBarPerspectiveMenuItem; }