<commit_before>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.java.ui.actions;

import java.lang.reflect.Method;

import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.ui.InterestFilter;
import org.eclipse.mylar.ui.MylarUiPlugin;
import org.eclipse.mylar.ui.actions.AbstractApplyMylarAction;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.internal.Workbench;

/**
 * This class is a bit weird since it doesn't obey the same contract as the other subclasses
 * 
 * @author <NAME>
 */
public class ApplyMylarToBrowsingPerspectiveAction extends AbstractApplyMylarAction implements IWorkbenchWindowActionDelegate{

	public static ApplyMylarToBrowsingPerspectiveAction INSTANCE;
	
	private String packageViewerWrapperClassName = "org.eclipse.jdt.internal.ui.browsing.PackageViewerWrapper";
	
	private String[] viewNames = { 	"org.eclipse.jdt.ui.MembersView",
									"org.eclipse.jdt.ui.PackagesView",
									"org.eclipse.jdt.ui.TypesView"
									};
	
	private String[] classNames = { "org.eclipse.jdt.internal.ui.browsing.MembersView",
									"org.eclipse.jdt.internal.ui.browsing.PackagesView",
									"org.eclipse.jdt.internal.ui.browsing.TypesView"
			};
	
	public ApplyMylarToBrowsingPerspectiveAction() {
		super(new InterestFilter());
		INSTANCE = this;
		prefId = PREF_ID_PREFIX + "javaBrowsing";
	}
		
	/**
	 * TODO: refactor duplicate behavior into super class
	 */
	@Override
    protected void valueChanged(IAction action, final boolean on, boolean store) {
        action.setChecked(on);
        
        if (store && MylarPlugin.getDefault() != null) MylarPlugin.getDefault().getPreferenceStore().setValue(prefId, on); 

        for(int i = 0; i < viewNames.length; i++){
        	StructuredViewer viewer = getBrowsingViewerFromActivePerspective(viewNames[i], classNames[i]);
        	if(viewer == null) continue;
        	if (on) {
				installInterestFilter(viewer);
				MylarUiPlugin.getDefault().getUiUpdateManager().addManagedViewer(viewer);
			} else {
				uninstallInterestFilter(viewer);
				MylarUiPlugin.getDefault().getUiUpdateManager().removeManagedViewer(viewer);
			}

        }
		refreshViewer();
	}
	
	@Override
	protected StructuredViewer getViewer() {
		return null;
	}

	@Override
	public void refreshViewer() {
        for(int i = 0; i < viewNames.length; i++){
        	StructuredViewer viewer = getBrowsingViewerFromActivePerspective(viewNames[i], classNames[i]);
        	if(viewer != null){
    			viewer.refresh();
    		} else {
    			MylarPlugin.log("Couldn't refresh null viewer: " + viewNames[i], this);
    		}
        }
	}

	public static ApplyMylarToBrowsingPerspectiveAction getDefault() {
		return INSTANCE;
	}

	private StructuredViewer getBrowsingViewerFromActivePerspective(String id, String className) {
		IWorkbenchPage activePage= Workbench.getInstance().getActiveWorkbenchWindow().getActivePage();
        if (activePage == null) return null;
        try {
            IViewPart viewPart = activePage.findView(id);
            Class sub = Class.forName(className);
            
            if (sub.isInstance(viewPart)) {
            	IViewPart view = viewPart;
        		if(view != null){
        			try{
        	            Class clazz = sub.getSuperclass();
        	            Method method= clazz.getDeclaredMethod("getViewer", new Class[] { });
        	            method.setAccessible(true);
        	            
        	            // TODO: weird since the packagesView uses a viewer that wraps another viewer<commit_msg>fixed Bug #107460: improve Java Browsing Perspective robustness<commit_after>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.java.ui.actions;

import java.lang.reflect.Method;

import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.ui.InterestFilter;
import org.eclipse.mylar.ui.MylarUiPlugin;
import org.eclipse.mylar.ui.actions.AbstractApplyMylarAction;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.internal.Workbench;

/**
 * This class is a bit weird since it doesn't obey the same contract as the other subclasses
 * 
 * @author <NAME>
 */
public class ApplyMylarToBrowsingPerspectiveAction extends AbstractApplyMylarAction implements IWorkbenchWindowActionDelegate{

	public static ApplyMylarToBrowsingPerspectiveAction INSTANCE;
	
	private String packageViewerWrapperClassName = "org.eclipse.jdt.internal.ui.browsing.PackageViewerWrapper";
	
	private String[] viewNames = { 	"org.eclipse.jdt.ui.MembersView",
									"org.eclipse.jdt.ui.PackagesView",
									"org.eclipse.jdt.ui.TypesView"
									};
	
	private String[] classNames = { "org.eclipse.jdt.internal.ui.browsing.MembersView",
									"org.eclipse.jdt.internal.ui.browsing.PackagesView",
									"org.eclipse.jdt.internal.ui.browsing.TypesView"
			};
	
	public ApplyMylarToBrowsingPerspectiveAction() {
		super(new InterestFilter());
		INSTANCE = this;
		prefId = PREF_ID_PREFIX + "javaBrowsing";
	}
		
	/**
	 * TODO: refactor duplicate behavior into super class
	 */
	@Override
    protected void valueChanged(IAction action, final boolean on, boolean store) {
        action.setChecked(on);
        
        if (store && MylarPlugin.getDefault() != null) MylarPlugin.getDefault().getPreferenceStore().setValue(prefId, on); 

        for(int i = 0; i < viewNames.length; i++){
        	StructuredViewer viewer = getBrowsingViewerFromActivePerspective(viewNames[i], classNames[i]);
        	if(viewer == null) continue;
        	if (on) {
				installInterestFilter(viewer);
				MylarUiPlugin.getDefault().getUiUpdateManager().addManagedViewer(viewer);
			} else {
				uninstallInterestFilter(viewer);
				MylarUiPlugin.getDefault().getUiUpdateManager().removeManagedViewer(viewer);
			}

        }
		refreshViewer();
	}
	
	@Override
	protected StructuredViewer getViewer() {
		return null;
	}

	@Override
	public void refreshViewer() {
        for(int i = 0; i < viewNames.length; i++){
        	StructuredViewer viewer = getBrowsingViewerFromActivePerspective(viewNames[i], classNames[i]);
        	if(viewer != null){
    			viewer.refresh();
    		} else {
//    			MylarPlugin.log("Couldn't refresh null viewer: " + viewNames[i], this);
    		}
        }
	}

	public static ApplyMylarToBrowsingPerspectiveAction getDefault() {
		return INSTANCE;
	}

	private StructuredViewer getBrowsingViewerFromActivePerspective(String id, String className) {
		IWorkbenchPage activePage= Workbench.getInstance().getActiveWorkbenchWindow().getActivePage();
        if (activePage == null) return null;
        try {
            IViewPart viewPart = activePage.findView(id);
            Class sub = Class.forName(className);
            
            if (sub.isInstance(viewPart)) {
            	IViewPart view = viewPart;
        		if(view != null){
        			try{
        	            Class clazz = sub.getSuperclass();
        	            Method method= clazz.getDeclaredMethod("getViewer", new Class[] { });
        	            method.setAccessible(true);
        	            
        	            // TODO: weird since the packagesView uses a viewer that wraps another viewer
{'fraction_non_alphanumeric': 0.13327247832040165, 'fraction_numerical': 0.0038795070743952533, 'mean_word_length': 3.5603537981269513, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3756937', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2506, 'n_words': 534}