blob: 17d228c9780ecdebb51b5839e0230f9d30555f98 [file] [log] [blame]
/**
* 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.apache.hdt.ui.internal.zookeeper;
import java.util.Iterator;
import org.apache.hdt.core.internal.model.ServerStatus;
import org.apache.hdt.core.internal.model.ZooKeeperServer;
import org.apache.hdt.core.internal.zookeeper.ZooKeeperManager;
import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.navigator.resources.ProjectExplorer;
public class ReconnectAction implements IObjectActionDelegate {
private final static Logger logger = Logger.getLogger(ReconnectAction.class);
private ISelection selection;
private IWorkbenchPart targetPart;
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
@Override
public void run(IAction action) {
if (this.selection != null && !this.selection.isEmpty()) {
IStructuredSelection sSelection = (IStructuredSelection) this.selection;
@SuppressWarnings("rawtypes")
Iterator itr = sSelection.iterator();
while (itr.hasNext()) {
Object object = itr.next();
if (object instanceof ZooKeeperServer) {
ZooKeeperServer r = (ZooKeeperServer) object;
if(logger.isDebugEnabled())
logger.debug("Reconnecting: "+r);
ZooKeeperManager.INSTANCE.reconnect(r);
if(logger.isDebugEnabled())
logger.debug("Reconnected: "+r);
if (targetPart instanceof ProjectExplorer) {
ProjectExplorer pe = (ProjectExplorer) targetPart;
pe.getCommonViewer().refresh(r, true);
}
}
}
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
* .IAction, org.eclipse.jface.viewers.ISelection)
*/
@Override
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
boolean enabled = true;
if (this.selection != null && !this.selection.isEmpty()) {
IStructuredSelection sSelection = (IStructuredSelection) this.selection;
@SuppressWarnings("rawtypes")
Iterator itr = sSelection.iterator();
while (itr.hasNext()) {
Object object = itr.next();
if (object instanceof ZooKeeperServer) {
ZooKeeperServer server = (ZooKeeperServer) object;
try {
enabled = server == null ? false : server.getStatusCode() == ServerStatus.DISCONNECTED_VALUE;
} catch (Throwable t) {
enabled = false;
}
} else
enabled = false;
}
} else
enabled = false;
action.setEnabled(enabled);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
* action.IAction, org.eclipse.ui.IWorkbenchPart)
*/
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.targetPart = targetPart;
}
}