All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.action.ActionProxy Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * *********************************************************************************************************************
 * 
 * SolidBlue - open source safe data
 * http://solidblue.tidalwave.it - hg clone https://bitbucket.org/tidalwave/solidblue-src
 * %%
 * Copyright (C) 2011 - 2014 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 * 
 * 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.
 * 
 * *********************************************************************************************************************
 * 
 * $Id$
 * 
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.netbeans.action;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.awt.EventQueue;
import javax.swing.Action;
import org.openide.util.Lookup;
import org.openide.util.Lookup.Item;
import org.openide.util.Lookup.Template;
import org.openide.util.actions.SystemAction;
import org.openide.util.lookup.Lookups;
import it.tidalwave.util.NotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@ThreadSafe @Slf4j
public final class ActionProxy 
  {
    private interface ActionLocator
      {
        @Nonnull
        public Action findAction()
          throws NotFoundException;
      }

    @RequiredArgsConstructor 
    private class LookupActionLocator implements ActionLocator
      {
        private final @Nonnull String category;

        private final @Nonnull String id;

        @Override @Nonnull
        public Action findAction() 
          throws NotFoundException
          {
            final String path = "Actions/" + category + "/";
            final Lookup pathLookup = Lookups.forPath(path);
            final Template actionTemplate = new Template<>(Action.class, path + id, null);
            final Item item = pathLookup.lookupItem(actionTemplate);

            return NotFoundException.throwWhenNull(item, "No action for " + path + id).getInstance();
          }
      }
    
    @RequiredArgsConstructor
    private class SystemActionLocator implements ActionLocator
      {
        private final @Nonnull Class actionClass;
        
        @Override @Nonnull
        public Action findAction() 
          throws NotFoundException
          {
            return SystemAction.get(actionClass);   
          }
      }
    
    @Nonnull
    private final ActionLocator actionLocator;
    
    public ActionProxy (final @Nonnull String category, final @Nonnull String id)
      {
        actionLocator = new LookupActionLocator(category, id);
      }
    
    public ActionProxy (final @Nonnull Class actionClass)
      {
        actionLocator = new SystemActionLocator(actionClass);
      }
    
    public void setEnabled (final boolean enabled)
      {
        EventQueue.invokeLater(new Runnable() 
          {
            @Override 
            public void run() 
              {
                try
                  { 
                    final Action action = actionLocator.findAction();
                    log.info("calling {}.setEnabled({})", action.getClass().getName(), enabled);
                    action.setEnabled(enabled);
                  }
                catch (NotFoundException e)
                  {
                    log.warn("setEnabled() failed: {}", e);  
                  }
              } 
          });  
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy